Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          Okay then.

          So we've seen how we can use loops to execute a block of code a certain number of times based on a certain
          
          condition.
          
          Now the other type of control flow is conditional statements, also known as if statements, and they're
          
          called if statements.
          
          Because we say if a certain condition is true, then we do something.
          
          So we check a condition and we only execute a code block If that condition is true.
          
          Now, this time we don't use any kind of counter variables because we only ever execute the code block
          
          once or not at all.
          
          We don't loop over the same code multiple times like we did in loops.
          
          So let's do an example.
          
          I'm going to create a const called Age and set that to be 20 to begin with.
          
          Now I'm going to do a conditional check, an if statement, and to do that we say if.
          
          Then in parentheses we do our condition, what do we want to evaluate?
          
          Well, we want to say if the age is over 20, then we have a code block at the end where we want to
          
          do something inside.
          
          And this code block is only going to execute if this condition is true.
          
          So inside here, we'll just say console, dot log, and then you are over 20 years old.
          
          Okay?
          
          So if we save this and preview in the browser, then we don't see anything and we don't see anything
          
          because this is not true.
          
          Age is not over.
          
          20 age is equal to 20.
          
          So if I change age now to 25, then this should evaluate to be true.
          
          And now we can see you are over 20 years old.
          
          Okay, so that there is an if statement.
          
          Now we could also do if statements with data.
          
          So to check if we have certain amount of items in an array or something like that.
          
          So I'll create a new constant and I'm going to call this ninjas and set this equal to an array.
          
          And inside I'll do a few different strings.
          
          So we have Sean and then we can have Ryu, then Chun-li, and then finally Yoshi.
          
          So we have four names in there.
          
          Now what I want to do is say if ninjas dot length is over three, then we'll execute some code.
          
          So it's going to grab the length of this, which is four.
          
          Is it over three?
          
          Well, yes it is.
          
          Therefore this evaluates to true and we can execute this code.
          
          So inside we're just going to log a message and this time I'm going to use double quotes and I'll tell
          
          you why in a second.
          
          I'm just going to say that's a lot of ninjas.
          
          Now, normally when I do strings, I use single quotes, but I did double quotes because there's a single
          
          quote inside the string right there.
          
          So if I used a single quote to do the entire string, then you'll notice that the string is actually
          
          closed here.
          
          So it's no longer used as an apostrophe, but to close the string instead.
          
          We don't want that.
          
          We want to use double quotes so that this single quote here does not close the string.
          
          Okay.
          
          So anyway, let's save that and run it.
          
          And we should see that in the console.
          
          Now, if I change this to be something like four, then the length is not greater than four and we don't
          
          see that statement.
          
          So these are I suppose, silly examples of why we'd use an if statement.
          
          Maybe we do something like this to check the length of some data, but typically we want to do something
          
          more useful.
          
          Like we want to check if a user is logged in.
          
          If they are, we do something or if they're signing up is the password that they provided us with eight
          
          characters long.
          
          Because if it's not, we want to send back an error and say improve your password or something like
          
          that.
          
          So what I'm going to do now is a very simple example const password and set it equal to a string.
          
          And the password that a user types in is going to be pass.
          
          Now that's a weak password.
          
          So what we're going to do is check if that password they enter is at least eight characters long.
          
          So I'm going to say if password dot length and we can do this on strings, remember to get back the
          
          length of the string and we're going to say if that is greater to or equal eight characters long, then
          
          we're going to log something to the console.
          
          Inside, we'll say that password is long enough.
          
          If it's not at least eight characters long, then it's not going to run this code.
          
          So if we save it, we can see it's not run this code.
          
          But if I change this now to something like password like this, we can see this is now eight characters
          
          long and this should run.
          
          So if I save it preview, we can see that password is long enough.
          
          Now, sometimes we might want to feed back some information if this code doesn't execute.
          
          For example, if it was this again and it was just four characters long and we saved it, we don't get
          
          any feedback whatsoever, it might be that we want to say that password is not long enough, and to
          
          do that we're going to have to talk about else statements.
          
          So we'll do that in the next lecture.