I have a string with repeated letters. I want letters that are repeated more than once to show only once. For instance I have a string aaabbbccc i want the result to be abc. so far my function works like this:
- if the letter doesn't repeat, it's not shown
- if it's repeated once, it's show only once (i.e. aa shows a)
- if it's repeated twice, shows all (i.e. aaa shows aaa)
- if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
var unique = '';
var count = 0;
for (var i = 0; i < string.length; i++) {
for (var j = i+1; j < string.length; j++) {
if (string[i] == string[j]) {
count++;
unique += string[i];
}
}
}
return unique;
}
document.write(unique_char('aaabbbccc'));
The function must be with loop inside a loop; that's why the second for is inside the first.