1
00:00:00,670 --> 00:00:06,250
Now that we've created our basic structure, we're ready to create the game we need to add in an event

2
00:00:06,250 --> 00:00:07,770
listener to the button object.

3
00:00:07,990 --> 00:00:11,770
So adding an event listener and the event that we want to listen for is a click.

4
00:00:12,040 --> 00:00:17,140
And once it gets clicked, let's create a function so anonymous function where we've got all of our

5
00:00:17,140 --> 00:00:20,910
start functionality and we're really going to have that one button in the game.

6
00:00:21,160 --> 00:00:24,980
So that's going to be the one that's going to have the ability to start the game.

7
00:00:25,600 --> 00:00:33,220
So looking through our code, we've got an array with my words and we've got a number of different words

8
00:00:33,220 --> 00:00:34,080
that are in that array.

9
00:00:34,330 --> 00:00:39,460
So to make this a little bit more interesting, we want a random order, sort this array.

10
00:00:39,970 --> 00:00:45,610
So taking the my words array, we want to sort that within a random order.

11
00:00:45,940 --> 00:00:52,120
If we use JavaScript sort, that gives us the ability to sort the elements in place.

12
00:00:52,360 --> 00:00:59,430
And you're going to see that once I sort that, if I console log out that my words array.

13
00:00:59,440 --> 00:01:04,690
So every time I click start, you're going to see that we're outputting the my words array and it's

14
00:01:04,690 --> 00:01:06,940
always now in alphabetical order.

15
00:01:07,240 --> 00:01:09,680
So it's taken the C, the J and the L..

16
00:01:10,060 --> 00:01:15,850
So although it did sort it outside of the order that we currently have, it's not exactly what we want

17
00:01:15,850 --> 00:01:16,030
it.

18
00:01:16,210 --> 00:01:22,800
So we won't have a callback function in here that we can return back and manipulate the sort order.

19
00:01:23,230 --> 00:01:31,330
So returning back those items that we're sorting, we can multiply it by by negative five and then do

20
00:01:31,330 --> 00:01:32,620
a math random.

21
00:01:32,800 --> 00:01:37,480
So this is going to give us a random value that's either going to be positive or negative.

22
00:01:37,490 --> 00:01:44,450
And when we return that value, you can see that now what's happening is a random sorting our array.

23
00:01:44,710 --> 00:01:49,480
So this is the quick and easy way to create a random sorting of your array.

24
00:01:50,840 --> 00:01:57,380
Using the sought method and adding in a function that's going to return back either a negative or a

25
00:01:57,380 --> 00:02:03,130
positive, and that's going to influence the sort order of the elements within your car.

26
00:02:03,470 --> 00:02:08,390
So that's going to influence the sort order of the items within your array.

27
00:02:08,660 --> 00:02:14,750
So now that we've picked out and we have random sorting, we have our starting word.

28
00:02:14,930 --> 00:02:19,820
So whatever the first one is going to be within the array, that's the word that we're going to use

29
00:02:19,820 --> 00:02:22,770
and that's the one that we're going to output to the player.

30
00:02:23,150 --> 00:02:26,510
So taking that word, we can just call it the word.

31
00:02:26,720 --> 00:02:29,840
And now we've got a randomly sorted array.

32
00:02:30,320 --> 00:02:32,630
We can use the shift method.

33
00:02:32,780 --> 00:02:37,640
And what shift does is it removes the first element from the array and returns that element.

34
00:02:37,970 --> 00:02:42,260
And what it's going to do is essentially into the word.

35
00:02:42,410 --> 00:02:45,050
It's going to place one of the values from the array.

36
00:02:45,440 --> 00:02:51,350
So now we can only press it three times because once we press it the fourth time, we only have three

37
00:02:51,350 --> 00:02:52,210
items in the array.

38
00:02:52,400 --> 00:02:58,430
So we come up with an undefined and as you can see, it's going to be a different sort order every time,

39
00:02:58,610 --> 00:03:01,180
and it's just simply removing them out of the array.

40
00:03:01,580 --> 00:03:05,770
So that gives us the ability to have a really random order of all of the content.

41
00:03:05,780 --> 00:03:10,090
And if you've got a lot of words in there, it's going to make it really interesting for the players.

42
00:03:10,310 --> 00:03:12,740
So go ahead and add that into your project.

43
00:03:13,040 --> 00:03:23,090
Try out the sort method with the return in order to randomly sort in place your array values and then

44
00:03:23,090 --> 00:03:28,290
also select out that first item and pull that out of your my words array.

45
00:03:28,400 --> 00:03:31,190
So coming up next, we're going to build out our board.

46
00:03:31,490 --> 00:03:35,240
And that's going to be the interesting part because we're actually going to see something within our

47
00:03:35,240 --> 00:03:36,870
browser other than a start button.

48
00:03:36,980 --> 00:03:38,600
So that's still to come, the next lesson.
