Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          All right then, gang.

          So we've explored a lot of different ways to control the flow of our code loops and then conditionals.
          
          Now, I briefly want to jump back to loops to explain a couple of key words in JavaScript, and those
          
          are break and continue.
          
          So imagine we have an array of scores which we do have right here and we want to loop through them.
          
          So that's why I've created a for loop.
          
          I'm saying for then initializing our counter variable to zero while I is less than the length of scores
          
          we're going to continue.
          
          And then each time around we're incrementing I by one.
          
          So inside this loop, all we're doing is logging out the score for each iteration, using scores, and
          
          then passing in the counter variable, which starts at zero.
          
          So here, then one, then two, then three, etcetera.
          
          So if we save and preview, then it's going to look something like this and it logs all the scores out.
          
          Now then what our break and continue.
          
          Well let's imagine if we get to a score of 100 when we're going through this loop when we get to that
          
          that is the max score you can get in this game.
          
          And at that point, we want to break out the loop because the rest of the scores after 100, they don't
          
          really mean anything because we've already reached the top score.
          
          So what we do is we'd come down here and we'll say if and scores and then square bracket notation I
          
          so the current score we're iterating over if that is equal to 100, so triple equals to 100, then we
          
          want to break out of the loop.
          
          Before we do that, let's log a little message to the console.
          
          We'll say, Congrats, you got the top score.
          
          And then what we're going to do is break out of the loop.
          
          So that basically means if we break out of the loop, we're not going to carry on iterating over the
          
          last two elements, even though I might still be less than the scores length this break keyword means.
          
          Now I just want to break completely from the loop, ignore all this stuff at the top and carry on with
          
          the rest of the code down here.
          
          So we're not going to see these two now logged to the console like we did before.
          
          So let me save it and preview this in a browser.
          
          And you see, when we get to 100, we get this message, then we break out of the loop and we don't
          
          do any more.
          
          There's no more iterations.
          
          So that's what the break keyword does.
          
          It breaks us out of the loop.
          
          So what about continue?
          
          Well, let's do another example.
          
          Say instead of 100, we now check for a score of zero.
          
          Now that's a pretty embarrassing score if you get zero.
          
          And we don't necessarily want to log that to the console because it's so embarrassing.
          
          So what we could do instead is say, okay, well, I want to check if the current score that we're iterating
          
          over so scores and then I if that is equal to zero, then what I'm going to do is use the continue keyword.
          
          And what that means is, okay, I want you to now ignore the rest of the code inside the code block
          
          for this loop, but I still want you to continue with the loop itself.
          
          So I want you to go back up to the top and go to the next iteration, increase I by one and go to the
          
          next iteration.
          
          So when the score is zero, it's going to run this, it's going to hit continue, ignore all of this
          
          code so we don't log it to the console and we don't need to perform this if check either.
          
          But we go back up to the loop and we go to the next number, the next iteration, because I is now increased
          
          by one.
          
          So that's what the continue does jumps out of that one particular loop that one iteration goes back
          
          to the top.
          
          So if we save this now, we shouldn't see zero log to the console after 25, we should just see 30.
          
          So save it.
          
          Go back to the console and we get 25, then 30, then 100.
          
          Okay, So there we go.
          
          My friends, that is break to break out of the loop completely and continue just to break out of the
          
          current loop and go back up to the top and continue with the next iteration.