FAQ: Loops - The break Keyword


#1

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:

Web Development

Introduction To JavaScript

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 (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.

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

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!


#2

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.


#3

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.


#4

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…


#5

Hello,
Can someone help me understand why my code doesn’t seem to work :confused:
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.");

#6

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


#7

That would be,

rapperArrayIndex < rapperArray.length;

#9

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


#10

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.


#12

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.”);


#14

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

// 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.”);*/


#15

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.