I have this array ["academy"]
and I need to count the characters in the string within the array.
The expected output should look like this:
a:2
c:1
d:1
e:1
m:1
y:1
To achieve this, I attempted using two for loops as shown below:
function sumChar(arr){
let alph = "abcdefghijklmnopqrstuvxyz";
let count = 0;
for (const iterator of arr) {
for(let i=0; i<alph.length; i++){
if(iterator.charAt(i) == alph[i]){
count++;
console.log(`${iterator[i]} : ${count}`);
count = 0;
}
}
}
}
console.log(sumChar(["abdulloh"]));
However, the function is not producing the correct output. Instead, it gives me:
a : 1
b : 1
h : 1
undefined