FAQ: Loops - The break Keyword

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.

I just updated my code to look similar to the answers above:

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

And now it works fine. The instructions aren’t clear. If the SCT is looking for the name in the loop parameters, the instructions should say something like:

Log each element from rapperArray in a for loop with the iterator variable i . Do it as if you didn’t know the number of elements that are stored in the array.

Hi @snailmachine
Yes! There’s a big difference. In your first example:

You first print the element that is in the corresponding index of the array and then check the condition. So that is why you are seeing in your console ‘Notorious B.I.G.’ In this lines of code, the code doesn’t care if rapperArray[i] is equal in value and type to ‘Notorious B.I.G.’; it just outputs the value and then proceeds to check the condition established. If it is met and since we have a break, the execution stops.

In your second example:

What the code is doing is first checking if rapperArray[i] is equal in value and type to ‘Notorious B.I.G.’ and since this condition will evaluate to false until the third iteration, it goes into the next lines of code, which are:

Outputting in the console the first to elements that are stored in the array. But then, we arrive at our third iteration, where the condition that you have set evaluates to true. Since it evalutes to true, it enters to the block of code that is within your conditional statement and proceeds to execute the break and this, in turn, stops completely the execution of your code; so this next line of code (which is after the break) will not execute:

console.log(rapperArray[i]);

If you would like to output ‘Notorious B.I.G.’ to the console, you would need to have a .log inside your conditional statement before the break. Like this:

if (rapperArray[i] === ‘Notorious B.I.G.’) {
console.log(rapperArray[i]);
break;
}

Hope this helps!

1 Like

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

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

5 posts were split to a new topic: Useful to introduce the use of the backslash

I think it might be useful to introduce the use of the backslash as differentiating between an apostrophe and a quotation mark such that:

So now we can dispense with full quotation marks in this code.

This website is definitely a Notorious B.I.G. fan . It breakes before tupac and hes excluded I wander why?
:laughing: :laughing: :laughing: :rofl:

The Break, set as you put it, works.

I did follow similar path to yours, earning fail, although made it work :smiley:


Logging to console renders Undefined fifth item before Break, in my example; x-1 sets it right. My code is probably too vague or I have an error and that is why it fails?!?

can be done looping thorugh an array

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

// Write your code below

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

Why is declaring iterator with var instead of let an invalid answer?

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

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

It successfully logs each element in the array and the first step only asks for that. Besides, as it turns out, my code is identical to the solution’s regarding part except I declared iterator with var instead of let. First step goes like this:
Log each element from rapperArray in a for loop with the iterator variable i .

The error i get is this: Did you create a for loop that loops through rapperArray ?

Sometimes we cannot work around the lesson checker so just have to humor it. If the keyword let is expected, then use it rather than argue the case for a defunct keyword it replaced.

I’m kinda new here, I thought I missed a point but I think it just happens., which kinda makes sense since there is a huge load of interactive content and it is only natural to experience such minor stuff.

Long story short, it is OK to use var and it is not a big deal :slight_smile:

Just so long as we understand the differences. var does not give block scope except within a function body.

1 Like