I'm trying to count the number of specific characters in a string, but am getting "0" instead of the correct output. I know there are easier ways to get the solution, but I want to know how to do it this way and what I'm doing wrong.
function countCharacter(str, char) {
var count = 0;
for(var i = 0; i < str.length; i++){
if(str.charAt(i) === char){
count++;
}
return count;
}
}
countCharacter("My cat is driving my crazy", "a");