Reputation: 1
My return variable keeps coming back undefined. Can someone please explain why? as far as I can tell it should be in scope.
var countBs = function(word) {
var count = 0;
for (var i = 0; i < word.length; i++){
if (word.charAt[i] == 'B'){
count += 1;
return count;
};
};
};
console.log(countBs('BBABBAB'))
Upvotes: 0
Views: 122
Reputation: 27792
You need to return count
at the end of the function, and use .charAt(i)
:
var countBs = function (word) {
var count = 0;
for (var i = 0; i < word.length; i++){
if (word.charAt(i) == 'B'){
count += 1;
};
}
return count;
}
Upvotes: 1