# Loops

#1

<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?

instructions:
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

console.log(countBs(“BBC”));
// → 2

``<do not remove the three backticks above>``

#2

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

#3

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!

#4

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

#5

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”)
else
return false
}
}

#6

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

#7

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
else
return false
}
}

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

FALSE

have i missed anything?

#8

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

#9

#10

Do you also understand why this changes are important?

you’re welcome

#11

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!?

#12

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:

``````console.log(countBs(“BBC”));
``````

#13

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!

#14

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

hope my explanations helped you

#15

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