I am trying to find the occurrences of specific characters in an array, but it seems the function is only looping through the array once as it is picking up the first count of a character, but not the subsequent ones.
Would anyone be able to give me a hint as to what is wrong please?
function trilingualDemocracy(group) {
let tempArray = group.split("");
let countD = 0;
let countF = 0;
let countI = 0;
let countR = 0;
for (let i = 0; i < tempArray.length; i++) {
if (tempArray[i] == "d") {
countD++;
} else if (tempArray[i] == "f") {
countF++;
} else if (tempArray[i] == "i") {
countI++;
} else if (tempArray[i] == "r") {
countR++;
}
return [countD, countF, countI, countR];
}
}
console.log(trilingualDemocracy("ffff"));
a return outside a function doesn’t work no. I meant to point out that you had it inside the loop, you need it outside the loop, but inside the function