1
00:00:00,520 --> 00:00:05,950
Great job on making it this far into the U.S. You've made it to your first challenge of the section

2
00:00:06,070 --> 00:00:08,740
and that's to create a random array message.

3
00:00:08,950 --> 00:00:15,220
And what we've been looking at in the earlier lessons with random is ideal in order to select random

4
00:00:15,220 --> 00:00:16,660
items from the array.

5
00:00:16,810 --> 00:00:22,670
So selecting the index, we can randomly select any value randomly from the array.

6
00:00:22,810 --> 00:00:28,990
So we create an array, have a bunch of items in that array, and then selecting out a value that randomly

7
00:00:28,990 --> 00:00:32,710
from the array and then output it within the document.

8
00:00:32,830 --> 00:00:36,790
So appending the child to creating a text node in the depending it to the body.

9
00:00:36,790 --> 00:00:39,940
Pause the video here and I'll walk you through the solution.

10
00:00:40,860 --> 00:00:45,780
Great, I hope you had an opportunity to try this out, so first thing that we need is an array.

11
00:00:45,930 --> 00:00:49,020
This can be an array of anything you want.

12
00:00:49,290 --> 00:00:56,400
In this case, I'm going to have some hello messages and a welcome message and also bye bye message.

13
00:00:56,670 --> 00:01:01,250
And we don't know which one we're going to get because this is going to be returned back at random.

14
00:01:01,710 --> 00:01:07,900
Let's create a function in order to select an item from random and then we want to pass in the array.

15
00:01:07,920 --> 00:01:13,470
So again, trying to make it dynamic as possible, because I know we've got the array out here, but

16
00:01:13,470 --> 00:01:15,360
it's always better to be able to pass it in.

17
00:01:15,390 --> 00:01:16,980
So that gives you more flexibility.

18
00:01:17,100 --> 00:01:22,530
If you've got multiple arrays, you can use the same function in order to generate and return back a

19
00:01:22,530 --> 00:01:23,980
random item from the array.

20
00:01:24,390 --> 00:01:29,400
So it using the array object, we're returning back a value one of the index values.

21
00:01:29,410 --> 00:01:36,420
So if we want to return back one there, you can see that when I refresh and a random item and in this

22
00:01:36,420 --> 00:01:42,570
case it's passed the array, you can see that we get welcome being returned back because welcome is

23
00:01:42,570 --> 00:01:43,400
index one.

24
00:01:43,590 --> 00:01:47,190
And we also just saw that we could generate a random value.

25
00:01:47,370 --> 00:01:52,890
So let's create temporary value and using a math flaw.

26
00:01:53,490 --> 00:01:59,120
So always use math flaw and math random in combination and then math random.

27
00:01:59,130 --> 00:02:01,650
And what do we want to multiply math random by?

28
00:02:01,800 --> 00:02:04,340
We want to multiply it by the length of the array.

29
00:02:04,590 --> 00:02:08,400
So we don't want to we don't want to avoid as much hard coding as possible.

30
00:02:08,640 --> 00:02:13,740
And math flaw is ideal when you're working with arrays because arrays are zero index.

31
00:02:13,920 --> 00:02:17,750
So zero one two and we're not going to have three.

32
00:02:17,760 --> 00:02:20,250
So that's where my flaw brings that down.

33
00:02:20,460 --> 00:02:26,180
So we're turning back whatever the index value is, or replace one with the index and move that in.

34
00:02:26,190 --> 00:02:28,410
Let's clean that up and refresh.

35
00:02:28,560 --> 00:02:32,970
And now whenever I select a random number, don't forget, we need to pass in the array.

36
00:02:33,240 --> 00:02:36,230
We can get a random item coming from the array.

37
00:02:36,600 --> 00:02:42,870
So there's another part to this challenge, and that was to use this function and output the content

38
00:02:42,870 --> 00:02:45,450
into our HTML so that it's visible.

39
00:02:45,450 --> 00:02:48,690
So every time we load the page, we're going to have a different value there.

40
00:02:48,990 --> 00:02:51,330
So let's create another variable here.

41
00:02:51,330 --> 00:02:59,160
We can also call that temp because this is out of the scope of the function and using the random item

42
00:02:59,370 --> 00:03:01,370
passan the array object.

43
00:03:01,980 --> 00:03:06,140
So now temp will have any one of the values that's being returned message.

44
00:03:06,180 --> 00:03:07,130
So document.

45
00:03:07,140 --> 00:03:12,690
So creating a text node and the contents of the text node will be whatever is being returned back from

46
00:03:12,690 --> 00:03:21,180
the function and then using document body, we can append the child to the body so we can do is append

47
00:03:21,180 --> 00:03:21,800
the message.

48
00:03:22,080 --> 00:03:28,320
So now whenever we refresh, you can see that you've got this random value and this is coming out of

49
00:03:28,320 --> 00:03:28,820
the array.

50
00:03:29,190 --> 00:03:32,370
So doesn't matter how many times you refresh it, you're always going to get something different.

51
00:03:32,460 --> 00:03:36,360
Well, of course, sometimes you do get the same one because we only have the three items in the array.

52
00:03:36,840 --> 00:03:37,580
So try this out.
