Now, in the last lesson, we created what is the basic structure of a love calculator using randomisation
and Javascript.
Now, wouldn't it be nice if we could give the user a custom message based on what they got as their love
score?
Now in order to do this we need a way of checking what score they got and responding differently based
on that score.
And to do that we need to use conditionals. The way that conditional statements work is, you can use the
keyword if to evaluate a statement.
For example, in this case, if the track is clear. And, if that statement is true, then it will carry out
a particular sequence of code.
But if that statement is false,
so the track is not clear,
then you can have an ELSE keyword that tells the computer what it should do instead
in that case. And this, in programming, is known as control flow. We're essentially controlling the flow
of our code depending on the current conditions.
By the way I had a lot of fun building that toy train set in Keynote. In case anybody else is living
in a cramped London apartment and you don't have the space for a full toy set, and you're probably too
old to play with them anyways,
Keynote is your best bet. Now,
this is how we might express our little track scenario in code.
So we have the keyword if,
and then we have some parentheses.
And inside the parentheses are some conditions.
So if the track is equal to clear then we take the flow of the code into the curly braces straight after
and then we run the function goStraight. However, if that condition is not true, i.e. track does not
equal clear, then the code flows to the else statement,
and we run the function turnRight instead.
While this is an easy way of seeing how our code works, it doesn't look very good when we have lots and
lots of code inside the if and else statement,
so more commonly you'll see this code structured like this by Javascript programmers.
So we say if, we test the condition, and that's followed straightaway by the block of code that will
get carried out if that condition is true, and then we open up the else statement to specify what should
happen if that condition is not true. And essentially, through the testing of these conditions, we get
to control the flow of code,
and that's why they're called conditionals. So let's use what we learned to edit our code in the love calculator.
So we have a love score here and we know that we get a number between 1 to 100.
So that will be the condition that we will test.
So we will say if, which is the keyword here, and then we'll open up some parentheses to add our testing
condition.
So let's say that our condition was that loveScore is equal to 100.
And in order to test for equality we're using three equal signs.
And if that is true then this block of code will be executed.
But if it's not true, i.e. else, then this block of code will be executed instead.
So let's say that, if loveScore was equal to 100.
Well that's pretty crazy, right?
Then we should say something like, alert "Your love score is " this amount.
And then we should give them an interpretation.
Like, for example, "You love each other like Kanye loves Kanye.".
But otherwise we just give them the plain old normal message where we tell them their love score.
Now some might have realized that I would have to go through this many many times until I happened to
get a love score that is 100, which is quite unlikely.
I mean it's 1 percent likely, right?
So what if we simply wanted to test for a range, say for example if the love score was between 70 and
100, or if the love score was between 0 and 30?
Well, in order to do that, then we would have to use something like greater than.
So if love score was greater than 70 then we should get this alert,
otherwise we get this simple one instead.
So let's try this out and we get 58 on the first go.
There we go.
So we finally got above 70.
We've got 76 percent.
And we get told that we love each other like Kanye loves Kanye.
I used to love Kanye.
I thought I was Kanye. What if Kanye made a song about Kanye?
Man that would be so Kanye.