1
00:00:00,750 --> 00:00:07,200
This lesson we're going to be introducing randomness into our JavaScript and randomness is almost a

2
00:00:07,200 --> 00:00:12,360
crucial part to any game because you want to create some type of unexpected result.

3
00:00:12,540 --> 00:00:15,440
And in this case, it's fairly simple what we're trying to do.

4
00:00:15,630 --> 00:00:23,190
We're asking the user to guess whether it's heads or tails using our math random in JavaScript to generate

5
00:00:23,190 --> 00:00:28,980
either heads or tails and depending on what the result is, then we check to see if the user guessed

6
00:00:29,040 --> 00:00:29,640
correctly.

7
00:00:29,730 --> 00:00:31,310
And if they did, then they win.

8
00:00:31,320 --> 00:00:33,440
If they didn't, then they got it wrong.

9
00:00:33,840 --> 00:00:34,710
So let's.

10
00:00:35,760 --> 00:00:45,450
Put in some instructions for the player within the message area, select either heads or tails and then

11
00:00:45,450 --> 00:00:49,760
now within the coin toss, this is where our content gets triggered.

12
00:00:49,890 --> 00:00:56,430
So we're moving the game along within the coin toss function and we're checking to see what button got

13
00:00:56,430 --> 00:01:03,240
clicked so that we can compare to see what actually got tossed using our random value.

14
00:01:03,600 --> 00:01:08,750
So let's create our random value using the left computer toss.

15
00:01:08,790 --> 00:01:12,960
We're going to use math, random here in math, random in JavaScript.

16
00:01:13,110 --> 00:01:18,570
What this is going to produce this going to produce a long number, zero dot and a whole bunch of decimal

17
00:01:18,570 --> 00:01:19,160
places.

18
00:01:19,440 --> 00:01:21,170
So we're going to have to fix that as well.

19
00:01:21,780 --> 00:01:27,810
I'm going to log out what the value of computer tosses so you can see that within the console.

20
00:01:28,020 --> 00:01:30,600
So pressing heads or tails doesn't matter.

21
00:01:30,690 --> 00:01:33,090
This is our computer toss value.

22
00:01:33,300 --> 00:01:39,120
So it's not very useful for us right now because we need to to show either a zero or a one.

23
00:01:39,420 --> 00:01:43,680
So let's bring this value down and get rid of all of the decimal places.

24
00:01:43,830 --> 00:01:46,530
And we can do this using math flaw.

25
00:01:47,190 --> 00:01:50,580
So math flaw will get rid of all the decimal places.

26
00:01:50,760 --> 00:01:56,640
And we're still going to have a problem here because when we refresh and if we go heads or tails, we're

27
00:01:56,640 --> 00:01:58,160
always landing on zero.

28
00:01:58,410 --> 00:02:04,500
So we need to have some type of multiplication in order to work our math, random value.

29
00:02:04,740 --> 00:02:07,890
And we saw that this produced a really large number.

30
00:02:08,010 --> 00:02:16,020
But if we multiply it by two, then it's going to produce a value of zero or one dot and a whole bunch

31
00:02:16,020 --> 00:02:18,810
of decimal positions down.

32
00:02:19,080 --> 00:02:23,050
And if we get rid of math, if we use math flaw, that will get rid of the decimals.

33
00:02:23,250 --> 00:02:27,300
So now we're going to be producing either a one or a zero randomly.

34
00:02:27,540 --> 00:02:35,250
So Heads is the first time we're clicking at zero zero one zero one one zero and so on.

35
00:02:35,580 --> 00:02:37,180
And the same thing for tails.

36
00:02:37,440 --> 00:02:39,450
So this is our computer toss.

37
00:02:39,720 --> 00:02:44,400
And next we need to check to see if it was heads or tails that was clicked.

38
00:02:44,610 --> 00:02:49,560
And then depending on whether it was heads or tails, then we can make the decision if they matched

39
00:02:49,560 --> 00:02:51,380
the computer toss or not.

40
00:02:51,750 --> 00:02:58,410
And what I'll do is I'm going to set up some variables here and within the coin array, we're going

41
00:02:58,410 --> 00:03:00,270
to have two values we'll have.

42
00:03:01,150 --> 00:03:01,660
Heads.

43
00:03:03,010 --> 00:03:10,210
And tails, I operate it the same way that it's presented on top of the buttons, so we've got heads,

44
00:03:10,540 --> 00:03:18,520
we've got tails within our coin array and we know that we can use these in order to prevent to provide

45
00:03:19,240 --> 00:03:20,380
a value back.

46
00:03:20,530 --> 00:03:22,630
So we're using our computer toss.

47
00:03:23,080 --> 00:03:29,750
We can use whatever the zero or the one value and select an item from our coin array.

48
00:03:30,160 --> 00:03:33,410
So let's do that and within the coin array.

49
00:03:34,210 --> 00:03:37,480
So now we're going to be returning back either heads or tails.

50
00:03:38,940 --> 00:03:44,980
So we've got our selection was heads and the computer selection is tails, both selected heads.

51
00:03:46,120 --> 00:03:52,500
I'll add in string value here as well so that we can see it visually being represented into the console.

52
00:03:53,430 --> 00:04:00,330
And I'm going to add in one here for the player so you can see which one the player selected and which

53
00:04:00,330 --> 00:04:01,850
one the computer selected.

54
00:04:02,310 --> 00:04:04,560
So player was heads, computer was heads.

55
00:04:04,590 --> 00:04:05,550
So that would be a win.

56
00:04:05,790 --> 00:04:08,920
And we see that this one, would it be a loss because they're not equal.

57
00:04:09,390 --> 00:04:14,610
So in the next lesson, we're going to apply the logic and make our calculations using a conditional

58
00:04:14,610 --> 00:04:17,340
statement, whether it matched up or not.

59
00:04:17,430 --> 00:04:19,560
And if it did, then the player gets a point.

60
00:04:19,560 --> 00:04:22,950
If it didn't, then the computer gets a point.

61
00:04:22,950 --> 00:04:23,830
So we'll do it that way.

62
00:04:24,150 --> 00:04:26,040
So that's still coming up in the upcoming lessons.
