FAQ: Loops - The break Keyword

This community-built FAQ covers the “The break Keyword” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise The break Keyword

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, I’m having a hard time understanding why the code:

for (let i = 0; i < 99; i++) {
if (i > 2 ) {
break;
}
console.log(‘Banana.’);
}

console.log(‘Orange you glad I broke out the loop!’);

outputs ‘banana’ 3 times.

Count the iterations before the break…

``````0
1
2
break
``````

Each iteration prints ‘banana’, and there are only three iterations regardless the upper limit on the loop.

1 Like

Thank you! I was thinking it would print banana for each instance that i > 2, but now I see that is what triggers the break. I was thinking we “broke” out 3 times, and was wondering why it didn’t do it when i = 3 through 98…

1 Like

Hello,
Can someone help me understand why my code doesn’t seem to work
I’ve been checking it all over again and cannot find where the problem is

//my code

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

// Write you code below
for
(let rapperArrayIndex=0;
rapperArrayIndex < rapperArray.length;
rapperArrayIndex++)
{ console.log(rapperArray[rapperArrayIndex]);

``````if (rapperArray[rapperArrayIndex] === 'Notorious B.I.G')
{break;}
}
console.log("And if you don't know, now you know.");``````

I literally thought the sane thing when they said "rapperArrayIndex

for (let rapperArrayIndex = 0; rapperArrayIndex < rapperArray; rapperArrayIndex ++){

}

but instead what i think what they referred to was rapperArrayIndex just being i.

I’m still looking for answers myself

1 Like

That would be,

``rapperArrayIndex < rapperArray.length;``
1 Like

You missed a dot after ‘G’ in if statement:

1 Like

My Code outputs “Lil’ Kim
Jay-Z
Notorious B.I.G.
And if you don’t know, now you know.” but is not recognized as a solution. Help?

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

// Write you code below

for (let rapperArrayIndex = 0; rapperArrayIndex < rapperArray.length; rapperArrayIndex++){

console.log(rapperArray[rapperArrayIndex]);

`````` if(rapperArray[rapperArrayIndex] === "Notorious B.I.G.") {
break;
``````

}

}

console.log(“And if you don’t know, now you know.”);

I see. If you follow the instructions and use “rapperArrayIndex” as your index/counter, it’s wrong. The solution provided is the same as my code, but uses “i”. Very irritating.

So can anyone explain why this code will not complete the tasks?

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

// Write you code below
for (let i = 0; i < rapperArray.length; i++) {
console.log(rapperArray[i]);
if (rapperArray[i] === “Notorious B.I.G.”) {
break;
}
}
console.log(“And if you don’t know, now you know.”);

1 Like

There seems to be some issue with this exercise. I was pulling my hair out and questioning everything so had just to get the solution to see what I was doing wrong but it would seem I was doing nothing wrong? I restarted the exercise (not before I copied and pasted the solution to compare) and recoded again and did the same as the solution just not using i naming convention and even when I did it was still incorrect?

I also used the exact copied and pasted solution provided and it did not resolve the exercise so I have reported a bug.

This is just for others like me, you ain’t going mad I promise

// code per instructions (relevant variable names etc)
// compared to the solution copied and pasted below (before restarted this exercise) is the same code just relevant variable names etc which has caused much confusion

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

// Write you code below
for(let rapperArrayIndex = 0; rapperArrayIndex < rapperArray.length; rapperArrayIndex++) {
console.log(rapperArray[rapperArrayIndex]);
if (rapperArray[rapperArrayIndex] === ‘Notorious B.I.G.’){
break;
}
}
console.log(“And if you don’t know, now you know.”);

// correction solution copied and pasted before reset exercise.
/*const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

// Write you code below
for (let i = 0; i < rapperArray.length; i++){
console.log(rapperArray[i]);
if (rapperArray[i] === ‘Notorious B.I.G.’){
break;
}
}
console.log(“And if you don’t know, now you know.”);*/

5 Likes

Thanks! you’re example got me through it.

``````const rapperArray = ["Lil' Kim", "Jay-Z", "Notorious B.I.G.", "Tupac"];

// Write you code below
for (let rapperArrayIndex = 0; rapperArrayIndex < rapperArray.length; rapperArrayIndex++){
console.log(rapperArray[rapperArrayIndex]);
if (rapperArray[rapperArrayIndex] === "Notorious B.I.G."){
break;
}
}
console.log("And if you don't know, now you know.");
``````

for whatever reason it didn’t like having the:

console.log(rapperArray[rapperArrayIndex]);

below the if statement despite that’s how it’s shown in the example. Once I moved it above it worked.

3 Likes

I have also experienced this inconsistency between the lesson and the instructions. The only difference between the solution code and my code was the placement of console.log(rapperArray[i]);, which contradicts the lesson.

2 Likes

I having problems to achieve console.log to display results, when code following i get no response, so I am wondering where in the scripting I am going wrong?

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

for (let i = 0; i < rapperArray.lenght; i++) {
console.log(rapperArray[i]);
};

1 Like

what you mean by dont shoot messenger?

I mean it’s a simple typo in your spelling of `length`.

The computer is saying that the code below does not create a for loop that logs through rapperArray (step 1), even though that is what my code is doing.

``````for (i = 0; i < rapperArray.length; i++){
console.log(rapperArray[i])
};
``````

quick question…why do we need to console log rapperArray[I] before If statement? How does that affect the functionality?

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

for (let i = 0; i < rapperArray.length; i++){
console.log(rapperArray[i]);
if (rapperArray[i] === ‘Notorious B.I.G.’){
break;
}
}
console.log(“And if you don’t know, now you know.”);

Why won’t this work? I’m only on step 1. This should work.

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];