This lesson we're going to be introducing randomness into our JavaScript and randomness is almost a
crucial part to any game because you want to create some type of unexpected result.
And in this case, it's fairly simple what we're trying to do.
We're asking the user to guess whether it's heads or tails using our math random in JavaScript to generate
either heads or tails and depending on what the result is, then we check to see if the user guessed
correctly.
And if they did, then they win.
If they didn't, then they got it wrong.
So let's.
Put in some instructions for the player within the message area, select either heads or tails and then
now within the coin toss, this is where our content gets triggered.
So we're moving the game along within the coin toss function and we're checking to see what button got
clicked so that we can compare to see what actually got tossed using our random value.
So let's create our random value using the left computer toss.
We're going to use math, random here in math, random in JavaScript.
What this is going to produce this going to produce a long number, zero dot and a whole bunch of decimal
places.
So we're going to have to fix that as well.
I'm going to log out what the value of computer tosses so you can see that within the console.
So pressing heads or tails doesn't matter.
This is our computer toss value.
So it's not very useful for us right now because we need to to show either a zero or a one.
So let's bring this value down and get rid of all of the decimal places.
And we can do this using math flaw.
So math flaw will get rid of all the decimal places.
And we're still going to have a problem here because when we refresh and if we go heads or tails, we're
always landing on zero.
So we need to have some type of multiplication in order to work our math, random value.
And we saw that this produced a really large number.
But if we multiply it by two, then it's going to produce a value of zero or one dot and a whole bunch
of decimal positions down.
And if we get rid of math, if we use math flaw, that will get rid of the decimals.
So now we're going to be producing either a one or a zero randomly.
So Heads is the first time we're clicking at zero zero one zero one one zero and so on.
And the same thing for tails.
So this is our computer toss.
And next we need to check to see if it was heads or tails that was clicked.
And then depending on whether it was heads or tails, then we can make the decision if they matched
the computer toss or not.
And what I'll do is I'm going to set up some variables here and within the coin array, we're going
to have two values we'll have.
Heads.
And tails, I operate it the same way that it's presented on top of the buttons, so we've got heads,
we've got tails within our coin array and we know that we can use these in order to prevent to provide
a value back.
So we're using our computer toss.
We can use whatever the zero or the one value and select an item from our coin array.
So let's do that and within the coin array.
So now we're going to be returning back either heads or tails.
So we've got our selection was heads and the computer selection is tails, both selected heads.
I'll add in string value here as well so that we can see it visually being represented into the console.
And I'm going to add in one here for the player so you can see which one the player selected and which
one the computer selected.
So player was heads, computer was heads.
So that would be a win.
And we see that this one, would it be a loss because they're not equal.
So in the next lesson, we're going to apply the logic and make our calculations using a conditional
statement, whether it matched up or not.
And if it did, then the player gets a point.
If it didn't, then the computer gets a point.
So we'll do it that way.
So that's still coming up in the upcoming lessons.