"Write a JavaScript function to get the number of occurrences of each letter in specified string." I've tried this way, but all my outputs are 0 and I really don't get why.
My idea was: Alphabetic order - so if one letter is the same with the next one, the counter increases. When it isn't the same, it logs the letter, how many times it appears and it resets the counter.
By the way, I don't know how to make it read the letters which appear only once. Can you help?
function count(string) {
let string1 = string.split("").sort().join("");
let counter = 0;
for (let i = 0; i < string.length; i++) {
if (string1[i] == string[i + 1]) {
counter++;
} else {
console.log(string1[i] + " " + counter);
counter = 0;
}
}
}
count("thequickbrownfoxjumpsoverthelazydog");