1
00:00:00,900 --> 00:00:05,280
We have an array where we need to select a random value from that array.

2
00:00:05,520 --> 00:00:12,330
Currently, we have three different items within that array, three different text strings, and we

3
00:00:12,330 --> 00:00:13,680
possibly could have more.

4
00:00:13,800 --> 00:00:20,730
So we want to make this as dynamic as possible as well as we don't want the user to know which element

5
00:00:20,730 --> 00:00:23,360
and which string value we're going to be outputting.

6
00:00:23,640 --> 00:00:25,910
And this is where we turn to Amarth random.

7
00:00:26,070 --> 00:00:34,200
This is a function in JavaScript that returns a number in the range of zero to one inclusive of zero,

8
00:00:34,200 --> 00:00:35,270
but not of one.

9
00:00:35,310 --> 00:00:42,140
So basically you're going to get return back a value that's zero decimal and a whole string of numbers.

10
00:00:42,360 --> 00:00:47,310
And here's some examples of what happens and how the output looks within math.

11
00:00:47,310 --> 00:00:47,850
Random.

12
00:00:48,240 --> 00:00:53,520
So over here, we're just simply got a math random and this is a typical number that gets returned.

13
00:00:53,790 --> 00:01:01,470
So if we do want to have a random number, that's a particular value that we need to multiply that number

14
00:01:01,620 --> 00:01:04,200
by the value that we want to have output.

15
00:01:04,380 --> 00:01:07,710
There's an example here of how to get a random integer.

16
00:01:07,830 --> 00:01:09,580
So minimum, maximum.

17
00:01:09,750 --> 00:01:12,240
So we've got minimum is the math seeling.

18
00:01:12,420 --> 00:01:15,510
Maximum is the math flaw plus minimum.

19
00:01:15,690 --> 00:01:21,480
So essentially what we're doing is we're doing math for multiplying it by math, random, and that's

20
00:01:21,480 --> 00:01:22,870
returning back the number.

21
00:01:22,920 --> 00:01:26,610
So let's go ahead and take a closer look at this within our ED.

22
00:01:27,790 --> 00:01:32,750
So going into play the game, first of all, let's create a random number.

23
00:01:33,900 --> 00:01:36,630
So we'll call it random number using math.

24
00:01:37,610 --> 00:01:44,870
I don't see what that's going to return, so a console log out the value of a random number, so refresh

25
00:01:44,870 --> 00:01:48,170
and every time we hit start, we get a random number.

26
00:01:48,320 --> 00:01:49,700
So it's got a lot of decimal.

27
00:01:49,700 --> 00:01:52,100
It's decimal with a lot of numbers.

28
00:01:52,440 --> 00:01:56,180
So we want to actually multiply that by a meaningful number.

29
00:01:56,330 --> 00:02:01,910
And if we want to have a number from zero to nine, we could multiply it by 10.

30
00:02:03,170 --> 00:02:09,980
So that would give us a random number, anywhere from zero to nine, and just to be sure, this is where

31
00:02:09,980 --> 00:02:16,100
we could do math, fluorosis, another builtin not function in JavaScript, and that would actually

32
00:02:16,100 --> 00:02:24,410
bring the returned value down to the floor to make sure that we're rounding it down to the nearest floor

33
00:02:24,410 --> 00:02:24,860
value.

34
00:02:25,010 --> 00:02:32,990
And as we saw over here, when we use floor, then this is the rounding a down to the nearest full number.

35
00:02:33,290 --> 00:02:37,030
Whereas if we do math ceiling, we're rounding up to the nearest full number.

36
00:02:37,250 --> 00:02:38,870
So that's what the difference there is.

37
00:02:39,380 --> 00:02:44,990
So right now, if we press it multiple times, you're going to see that we're always returning the number

38
00:02:44,990 --> 00:02:47,220
back anywhere from zero to nine.

39
00:02:47,420 --> 00:02:48,350
So there's nine.

40
00:02:48,350 --> 00:02:51,070
And if we keep going, we see that we've got a zero.

41
00:02:51,260 --> 00:02:52,550
So we never get 10.

42
00:02:52,670 --> 00:02:58,840
If we do want to have 10 that we can add plus one, this will return back a number from one to 10.

43
00:02:59,390 --> 00:03:01,820
And as you can see, 10 got returned back there.

44
00:03:02,280 --> 00:03:09,770
And if we want to get a random item from our array, we know that this is an array and arrays have length.

45
00:03:10,020 --> 00:03:16,370
So instead of 10, we could do wording Dorte length and this is going to be equivalent to three.

46
00:03:16,520 --> 00:03:21,830
I'm going to get rid of that zero one because arrays are zero based and we want to actually return a

47
00:03:21,830 --> 00:03:24,940
number back anywhere from zero to two.

48
00:03:25,460 --> 00:03:30,230
And as you can see, the numbers being returned back are always within that range, within a random

49
00:03:30,230 --> 00:03:30,620
number.

50
00:03:30,840 --> 00:03:32,700
You could try it out for yourself as well.

51
00:03:33,200 --> 00:03:36,960
So the next thing that we want to do is output within that message.

52
00:03:37,490 --> 00:03:41,930
So taking message and updating the inner text of message.

53
00:03:43,540 --> 00:03:45,520
We're going to use the wording, ouray.

54
00:03:46,440 --> 00:03:53,010
And specify an index value and that index value is going to be equivalent to that random value.

55
00:03:53,340 --> 00:04:00,660
So essentially, as we saw, we're getting a value of zero one or two because we have three items within

56
00:04:00,660 --> 00:04:01,250
the array.

57
00:04:01,500 --> 00:04:05,580
So that's indicated by that length index values from the array.

58
00:04:05,760 --> 00:04:12,210
And this is dynamically so in case our rate increases in length, our formula will always still work.

59
00:04:12,540 --> 00:04:14,160
So go ahead and try it out for yourself.

60
00:04:14,340 --> 00:04:20,160
That's not random and try some of the examples, generate some random numbers and output in the console.

61
00:04:20,430 --> 00:04:26,850
And coming up next, we'll show you how you can update the start button as well as how we can set a

62
00:04:26,850 --> 00:04:29,910
start time when the user presses the start button.

63
00:04:30,090 --> 00:04:35,880
And then another time that we can get when the user completes typing and hits the end button.

64
00:04:36,270 --> 00:04:37,710
So all of that is still yet to come.
