Been stuck on this problem for a bit and need help:
Given an string, count how many times each letter occurs inside a given string; the letters must stay in order of the way the appear in the string with the count next to them. The return must be a single string with letters and count.
Example:
for example: "apple" is "a1p2l1e1", "tees" is "t1e2s1"
I got kinda close with the output format but not quite? this is my code:
function countLetters(str){
let newArr = []
let i = 0;
while(i < str.length){
if(str.includes(str[i])){
newArr += str[i];
newArr += 1;
i++;
}
}
return newArr;
};
console.log(countLetters("apple")); // "a1p2l1e1"
and this is my output:
"a1p1p1l1e1"
Any ideas?