FAQ: Loops - The break Keyword

A post was split to a new topic: Solution is wrong? [self-solve]

A post was split to a new topic: Code Problem? [Unreproducible]

9 posts were merged into an existing topic: [Bug Reported] Solution code not advancing lesson

4 posts were split to a new topic: Typo in code? [Solved]

A post was merged into an existing topic: [Bug Reported] Solution code not advancing lesson

2 posts were split to a new topic: Difference between “do” and “for” looping [Answered]

I think you guys should add, continue statement to this break chapter

3 Likes

My code for this exercise was:

for (let i = 0; i <= 3; i++) {
  if (i < 3) {
    break;
  }
  console.log(rapperArray[i]);
};
console.log("And if you don't know, now you know.");

When I hit “run” it worked, but the exercise was marked as failed. I do see the “correct” solution, but, what in my code is wrong? I know it would be better to match to BIG since the place in the array can change, but, for the purpose of the exercise I thought this would be okay.

1 Like

In the exercise we are to log the rapper in each iteration and break after we’ve logged, 'Notorious B.I.G.'.

console.log(rapperArray[i]);
if (rapperArray[i] === 'Notorious B.I.G.') {
    break;
}
3 Likes

Once I heard that using break, continue, and keywords that would forcibly change the regular logic are not part of good programming practices. Can someone sahre their wisdom on the matter? Thanks!

1 Like

If they were not part of good practice then why would they exist? We may be able to write code other ways so that we avoid their use, but are they really something to avoid?

Their purpose is simple, based upon a given conditional state, either allow the loop to continue, or force it to terminate immediately. That sounds like an intentional part of the logic, not a forced change of it. They do affect the flow, though.

1 Like

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

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

^^

Hey guys, the exercise marked me as failed too. Just wondering what am I doing wrong here? These are my code.

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

// Write your code below
for (let i = 0; i < 4; i++) {
  console.log(rapperArray[i]);
}

is this an intermediate step, or is the conditional missing from the code?

2 Likes

Hello there @mtf, this code is just for the first step asking us to create a for loop logging the elements from rapperArray.

1 Like

Perhaps the SCT is looking for the name in the loop parameters…

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

}
6 Likes

That is precisely what it’s looking for… it was careless of me to miss that. Thank you so much @mtf! :pray::pray:

2 Likes

Hey guys, this is just part of my curiosity. I manage to solve all of them, but was just curious in the end. I experimented the placement of the “console.log” statements and found different results.

I found that, if the “console.log” statement comes before the condition, it includes the element 'Notorious B.I.G."

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.")

But, if it comes after the condition, it doesn’t include the element ‘Notorious B.I.G.’ Just wanted to find out if there’s any difference in this? Thank you!

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

That is curious, because the exercise accepted me adding a console.log before the break to add the 3rd element to the console. But I’m assuming that the code is executed in order, so if you have the log after the check statement, it doesn’t print it because it broke off that part of the code too. In other words, the program does this order: index check, if check, print to console, add one to i, run index check, if check, print to console etc. until if condition is met, then it breaks all code within the for loop. Adding the console.log before the break, or if statement entirely, will make sure that the program will print that index to the console before checking the if statement, or executing the break…

I’m working through the first instruction in the Break Keyword lesson, which is:

Log each element from rapperArray in a for loop with the iterator variable i .

So my code is like this:

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

// Write your code below
for (let i = 0; i < 4; i++) {
console.log(rapperArray[i]);
}

The expected output in the console should be all the elements in the array and that’s what happens, as it can be seen in the image I upload; but nonetheless I get it wrong and I get this red message:

Did you create a for loop that loops through rapperArray ?

I don’t know why I still get it wrong.