<Below this line, add a link to the EXACT exercise that you are stuck at.>

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Hey, everyone, i’d like to create a loop which can help me count how many uppercase “B” in the string, but i was kinda stuck right here, dunno what I need more for my codes?

Write a function countBs that takes a string as its only argument and returns a number that indicates how many uppercase “B” characters are in the string.

``` function countBs(words) { var x = String(words); for (i=0;i<x.length;i++){ if (x.charAt(i) ==="B") return

// → 2

<do not remove the three backticks above>


you need a variable count or total (whatever you like) and give a value of zero, now every time a B is found, increase count/total by one


Hi, really appreciate, but dunno where i should add a variable which has the name you mentioned, can you please us my code to show me how it works? really appreciated!


declaring variables is one of the first lessons?

well, we want our function to return the total numbers of B’s, so total should be declared inside the function and before the loop


Ok…so it should look like this!?(below)

function countBs(words) {
var x = String(words);
var total = 0;
for (i=0;i<x.length;i++){
if (x.charAt(i) ===“B”)
return total=total+1
return false


its a start

a function ends the moment a return keyword is reached

so you want to increase total, then after the loop return total


wait a second, so it should look like this!?

function countBs(words) {
var x = String(words);
var total = 0;
for (i=0;i<x.length;i++){
if (x.charAt(i) ===“B”)
total = total +1
return false
return total;

but the results of this console.log(countBs(“BBC”)) :


have i missed anything?


yea, get rid of else return false, a function ends the moment a return keyword is reached

so currently your function ends in the first iteration of the loop and returns false, something which we do not want to return in the first place


Yeah, i really made it, really appreciated !!!so glad i can have your help!


Do you also understand why this changes are important?

you’re welcome :slight_smile:


Well, i am kinda still a bit confused about the difference between “return” the variable inside the loop and outside the loop, can you please narrate them for me a bit!?


we declare a variable total and give it a value of zero, so we have a variable which can count (by increasing the variable)

then inside the loop, we use an if condition to determine if a match is found (B in this case), if so, increase count to keep track of the numbers of B’s

then after the loop, return the count (number of B’s in string)

then log the returned total to console by wrapping the function call in console.log:



Gotcha, thanks, sorry, i am just a beginner without any background knowledge, so all I can do is to ask the questions that I don’t understand totally; if bothered, really sorry!


well, we all have been there. We can’t be experts from the start

hope my explanations helped you :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.