1
00:00:00,240 --> 00:00:06,310
This is the randomise array lesson where we need to randomize one of the items from the array.

2
00:00:06,420 --> 00:00:12,180
So we need to pick one of these words and then we also need to scramble the letters in the word and

3
00:00:12,180 --> 00:00:13,680
I'll show you how to do all of this.

4
00:00:13,950 --> 00:00:19,860
Within this lesson, we're going to turn these strings into arrays and then we're going to randomize

5
00:00:19,860 --> 00:00:21,930
the order of the items in the array.

6
00:00:22,770 --> 00:00:28,200
So we see that we've got our inplay and then this is where we need to build out our word.

7
00:00:28,320 --> 00:00:32,020
So we'll call it create word and create a function for that.

8
00:00:32,400 --> 00:00:37,800
So the function and we'll consult log this out for now and then we're going to update that to output

9
00:00:37,980 --> 00:00:39,540
to the inner HTML.

10
00:00:39,960 --> 00:00:42,930
So create a function that can create the word.

11
00:00:42,930 --> 00:00:47,930
And then using JavaScript, we're going to select one of the elements at random.

12
00:00:48,180 --> 00:00:51,150
So selecting random index.

13
00:00:51,300 --> 00:00:58,050
Now we need to use math random in order to generate a random number and we're going to multiply it by

14
00:00:58,050 --> 00:01:00,870
the length of what our array is.

15
00:01:01,170 --> 00:01:04,620
So that's the array that we've got all of our content and we had called it Mairie.

16
00:01:04,620 --> 00:01:09,900
So selecting that array and we know that all arrays will have a value there of length.

17
00:01:10,170 --> 00:01:13,490
Let's do random index and see what's going to be output now.

18
00:01:13,950 --> 00:01:15,450
So we will refresh it.

19
00:01:15,660 --> 00:01:23,040
You see that we've got a two and we've got a bunch of decimal bunch of places there after that.

20
00:01:23,050 --> 00:01:27,570
So three and a whole bunch of digits after that, like Dorte, digits after that.

21
00:01:27,990 --> 00:01:33,900
And if you're familiar with JavaScript and math, you know that if you use math flaw, that will get

22
00:01:33,900 --> 00:01:35,710
rid of all that excess.

23
00:01:36,660 --> 00:01:42,390
So basically, it will strip away all of the decimal and all the excess characters, leaving us with

24
00:01:42,390 --> 00:01:45,800
a number from zero all the way up to.

25
00:01:45,810 --> 00:01:48,630
And in this case, we've got five items as a length.

26
00:01:48,900 --> 00:01:51,290
So it's going to be zero to four.

27
00:01:51,330 --> 00:01:52,890
We're going to be returning back of value.

28
00:01:53,130 --> 00:02:00,060
If you want to select random value from one to five, then you would have to add a plus one to this

29
00:02:00,420 --> 00:02:00,930
function.

30
00:02:01,620 --> 00:02:07,070
So now we've got a random number anywhere from zero to four and we see zero.

31
00:02:07,200 --> 00:02:08,640
We saw four earlier.

32
00:02:08,670 --> 00:02:12,240
We saw one, two, and eventually we would get three as well.

33
00:02:12,550 --> 00:02:19,300
So we're getting our random index and that allows us to select a random value from the my array.

34
00:02:19,320 --> 00:02:21,780
So let's call it temp word.

35
00:02:22,110 --> 00:02:28,530
And this is the word that we're going to be using using the mihiri, adding in the random index value

36
00:02:28,740 --> 00:02:30,120
that we've just generated.

37
00:02:30,900 --> 00:02:35,010
We're going to selecting one of these array words at random.

38
00:02:35,580 --> 00:02:37,440
And I'll show you that this way.

39
00:02:37,470 --> 00:02:41,420
We're selecting one of those words at random and we can output that into the console.

40
00:02:42,300 --> 00:02:45,210
So refresh start and we see that.

41
00:02:45,210 --> 00:02:47,140
We see we're getting a random word.

42
00:02:47,160 --> 00:02:48,630
So refresh start.

43
00:02:48,660 --> 00:02:51,360
Now we got document and we're getting an undefined there.

44
00:02:51,510 --> 00:02:53,660
And that means that we're not returning anything.

45
00:02:53,670 --> 00:03:00,150
So let's go ahead and return that temp word value so that we can also see that within the other console

46
00:03:00,150 --> 00:03:00,570
message.

47
00:03:01,140 --> 00:03:05,730
So now we got course on that one start again, of course, on this one and so on.

48
00:03:05,910 --> 00:03:08,370
So we're generating one of those words at random.

49
00:03:08,370 --> 00:03:11,250
And it just so happened that we got course three times in a row.

50
00:03:11,370 --> 00:03:13,990
But as you can see, we can get other values as well.

51
00:03:14,460 --> 00:03:16,320
So now we've got a random word.

52
00:03:16,330 --> 00:03:23,340
And next, we need to scramble the words, the letters in this word, and we're going to use JavaScript

53
00:03:23,340 --> 00:03:24,840
function in order to do that.

54
00:03:24,840 --> 00:03:28,410
And we'll call it function random array to be passed in.

55
00:03:28,440 --> 00:03:34,610
When we use JavaScript, we can turn any string into an array using split.

56
00:03:35,400 --> 00:03:41,820
So if we take temp word and if we split this using the JavaScript split method and then this is two

57
00:03:42,150 --> 00:03:46,860
double quotes there you can see that we're going to be splitting that out and it's going to be turning

58
00:03:46,860 --> 00:03:47,680
it into an array.

59
00:03:48,150 --> 00:03:55,710
So now our JavaScript word is in an array and that means that we can use the random array function,

60
00:03:55,710 --> 00:04:01,230
which we're going to set up in the upcoming lesson to reorganize the items in the array.

61
00:04:01,500 --> 00:04:03,120
And you can see that all the time.

62
00:04:03,120 --> 00:04:04,180
It turns it into an array.

63
00:04:04,650 --> 00:04:06,370
So coming up next, I'll show you how to do that.

64
00:04:06,720 --> 00:04:14,370
So for this lesson, go ahead and build the random selection from the array and then output that random

65
00:04:14,370 --> 00:04:21,690
string value, random item from the array, then split it into an array that we can use and work with

66
00:04:21,690 --> 00:04:22,910
in the upcoming lesson.

67
00:04:23,100 --> 00:04:25,020
So go ahead and add that into your application.
