1
00:00:00,210 --> 00:00:05,010
Within this lesson, we are randomizing the contents of an array and then we're going to be returning

2
00:00:05,010 --> 00:00:12,600
back that array, newly, freshly created array that we are building out, we created a function already

3
00:00:12,600 --> 00:00:15,900
called random array, and it's expecting an argument.

4
00:00:16,050 --> 00:00:19,800
And that argument that it's expecting is an array that gets passed in there.

5
00:00:20,100 --> 00:00:27,600
So now we're going to have our random array and will update this to be random or and then we can output

6
00:00:27,600 --> 00:00:32,960
and console, lagrand this word, the string value that's turned into an array.

7
00:00:32,970 --> 00:00:38,320
We can see that here in the right hand side in the console, reorganize the contents of the array.

8
00:00:38,430 --> 00:00:45,240
Let's set up our loop to loop through the contents of the array, set up a variable called AI and we're

9
00:00:45,240 --> 00:00:53,340
going to equal AI to the value of our array length, subtracting one quasar array length.

10
00:00:53,370 --> 00:00:56,880
So now we get the index values of the items in the array.

11
00:00:57,040 --> 00:01:01,890
Thus we need to subtract one and we're going to loop through while AI is less than zero.

12
00:01:02,250 --> 00:01:08,280
So once it hits zero, then that's where once it goes below zero, then that's where we stop the loop.

13
00:01:09,000 --> 00:01:14,940
And as long as I is above zero and that means that we need to decrease the value of AI.

14
00:01:15,090 --> 00:01:19,350
So a lot of the loops that we've done before are going to be increasing and in this case we're going

15
00:01:19,350 --> 00:01:20,900
to be decreasing that value.

16
00:01:22,540 --> 00:01:28,750
And we're also going to need to set up a temporary value for that item within the array so that we can

17
00:01:28,750 --> 00:01:35,120
store that and take that out and then store that as a new value as we build out the new temporary array.

18
00:01:35,890 --> 00:01:39,940
And for now, what I'll do is I'll consult, logout the value of temp.

19
00:01:40,740 --> 00:01:48,210
We also need to generate a random value, so called that G using what we did before with math flaw,

20
00:01:48,480 --> 00:01:54,240
that we're going to multiply it by whatever the value of AI is plus one.

21
00:01:54,480 --> 00:02:02,220
And remember, AI is decreasing in value as we're iterating through and a at the values of AI as well.

22
00:02:02,400 --> 00:02:07,220
And we'll consult log out the value of G so that we can see everything that's happening here.

23
00:02:07,620 --> 00:02:14,760
So as we Head Start, start out with the last letter and as we loop through and iterate through, AI

24
00:02:14,760 --> 00:02:21,330
is decreasing in value and we're also getting our random different value every time we're decreasing.

25
00:02:21,450 --> 00:02:24,720
So we can try that one more time and you can see that temp.

26
00:02:24,840 --> 00:02:27,450
So that temp is the letter that we're on.

27
00:02:27,810 --> 00:02:31,200
AI is the current index value that we're looking at.

28
00:02:31,230 --> 00:02:32,550
So that's what we're getting E from.

29
00:02:32,790 --> 00:02:38,420
And then G is the new random index value that we want to add into the array.

30
00:02:38,670 --> 00:02:40,890
So let's build out our new array.

31
00:02:42,860 --> 00:02:48,080
And we're just swapping the order between the two different items in the array, so we're taking the

32
00:02:48,080 --> 00:02:55,520
value of I and in this case, we're going to take E and we're going to equal E to whatever the value

33
00:02:55,520 --> 00:02:57,280
of J is.

34
00:02:57,290 --> 00:03:00,380
And in this case, it would be the second item in the index.

35
00:03:01,700 --> 00:03:09,380
So we're equaling those and then we're taking the value of J and we're equaling it to the value of temp.

36
00:03:09,680 --> 00:03:16,310
So basically we're doing a quick swap over of these characters as we're iterating through, and that's

37
00:03:16,310 --> 00:03:21,560
swapping around of the order of the characters is going to reorganize that current array.

38
00:03:21,740 --> 00:03:25,430
And then lastly, what we're going to do is we're going to return that array.

39
00:03:25,730 --> 00:03:29,020
And I would put that as a return value.

40
00:03:29,330 --> 00:03:31,220
So let's try that out and hit start.

41
00:03:31,460 --> 00:03:37,190
And you can see that now we've got a reorganized sorting of the contents of that array.

42
00:03:37,490 --> 00:03:43,160
And so this is a website and we've reorganized the items in the array and now it's just a matter of

43
00:03:43,160 --> 00:03:45,500
turning it back into a string value.

44
00:03:45,710 --> 00:03:50,140
So right now, we're getting a return to value and we can use joint.

45
00:03:50,210 --> 00:03:53,570
So we're joining it together with that's double two double quotes.

46
00:03:53,580 --> 00:04:00,230
And now when we refresh, we hit start and we get a scrambled version of whatever word was sent into

47
00:04:00,230 --> 00:04:03,790
the scrambler and this randomising of the array order.

48
00:04:04,010 --> 00:04:08,640
This is a function that I used very often was in a lot of different applications.

49
00:04:08,640 --> 00:04:14,630
So it's a very useful function, creating a random value and then swapping it with whatever that randomized

50
00:04:14,630 --> 00:04:15,470
item was.

51
00:04:15,650 --> 00:04:21,380
And it's doing it whatever number of characters we have in the array, essentially reorganizing the

52
00:04:21,380 --> 00:04:22,240
array content.

53
00:04:22,520 --> 00:04:24,380
So go ahead and try this out for yourself.

54
00:04:24,560 --> 00:04:29,690
Add in the ability to randomize the order of an array and that's going to move us along to the next

55
00:04:29,690 --> 00:04:35,090
step where we're almost ready to output that word to the player so that they can start guessing what

56
00:04:35,090 --> 00:04:36,820
the word is that we've generated.

57
00:04:37,100 --> 00:04:38,300
So go ahead and give it a try.
