I have a question concerning javascript. I have created a function which prompts the user to enter a string, and once the string is entered will perform a for loop to go through the entire length of the string, looking for the character "B" and then return the number of "B"s found in the result variable. As far as my knowledge, I feel that the code I have should work, however not matter how many "B" are present within my code, the function consistently returns 2. Any ideas on why this would be happening
function countBs(string) {
var result = 1;
var string = prompt("Please Enter a String");
for (count = 0; count < string.length; string++) {
if (string.charAt(count) == "B") {
result += 1;
return result;
}
}
};
count++notstring++for, not at the end of the function.