1 00:00:00,000 --> 00:00:01,040 Welcome back. 2 00:00:01,040 --> 00:00:03,486 How did you feel about writing this method? 3 00:00:03,486 --> 00:00:05,928 Don't worry if you had any trouble, the solution for 4 00:00:05,928 --> 00:00:09,028 this method is up on screen and I'm going to walk you through it now. 5 00:00:09,028 --> 00:00:12,944 I'm inside my player.js file where I wrote the method. 6 00:00:12,944 --> 00:00:19,815 As you can see, just below my constructor method, I created the method createTokens. 7 00:00:19,815 --> 00:00:21,473 It accepts one parameter, num. 8 00:00:21,473 --> 00:00:26,598 Num represents the number of token objects to be created. 9 00:00:26,598 --> 00:00:30,998 Inside the method I created a variable called tokens 10 00:00:30,998 --> 00:00:33,798 that's equal to an empty array. 11 00:00:33,798 --> 00:00:37,215 This array will be populated in the following lines of code, 12 00:00:37,215 --> 00:00:41,246 with all of our new token objects, and the array will then be returned. 13 00:00:41,246 --> 00:00:46,146 After creating the empty array, I wrote a for loop that will iterate num times. 14 00:00:48,156 --> 00:00:51,122 Inside the for loop, I do two things, 15 00:00:51,122 --> 00:00:56,985 first I create a variable called token and set it equal to new Token object. 16 00:00:56,985 --> 00:00:58,779 This is where the loop index and 17 00:00:58,779 --> 00:01:03,066 the owning player object are passed to the token's constructor method. 18 00:01:03,066 --> 00:01:08,828 After that, I add the newly created token to my tokens array using the push method. 19 00:01:08,828 --> 00:01:12,652 When the loop is finished and all the tokens have been created, 20 00:01:12,652 --> 00:01:13,887 I return the array. 21 00:01:13,887 --> 00:01:16,671 But where's it being returned to? 22 00:01:16,671 --> 00:01:18,190 Part of your assignment for 23 00:01:18,190 --> 00:01:22,822 this step was to call the create tokens method from inside the constructor method. 24 00:01:22,822 --> 00:01:27,769 By changing the tokens property to be set to the return value from the method call. 25 00:01:27,769 --> 00:01:32,256 If you jump up to the constructive method, you can take a look at this. 26 00:01:32,256 --> 00:01:38,416 As you can see I've set the token property to the return value from the method call. 27 00:01:38,416 --> 00:01:42,284 Note how when I call the create tokens method, I'm using the this keyword. 28 00:01:42,284 --> 00:01:46,970 Indicating that the method I want to call is available on the object that we're 29 00:01:46,970 --> 00:01:47,966 initializing. 30 00:01:47,966 --> 00:01:52,751 I've also passed the number 21 as an argument to the createTokens method. 31 00:01:52,751 --> 00:01:55,621 21 might seem like an arbitrary number, but 32 00:01:55,621 --> 00:02:00,405 you'll see later when we write the code that builds out the board and spaces, 33 00:02:00,405 --> 00:02:04,243 that 21 is equal to half of the number of spaces on the board. 34 00:02:04,243 --> 00:02:06,657 Each player gets 21 tokens total. 35 00:02:06,657 --> 00:02:10,082 So that the game board can be completely filled without 36 00:02:10,082 --> 00:02:11,950 having any tokens left over. 37 00:02:11,950 --> 00:02:16,110 If you wanted you could make the game more difficult by reducing the number of 38 00:02:16,110 --> 00:02:16,631 tokens. 39 00:02:16,631 --> 00:02:20,429 This would limit the number of opportunities that a player had to 40 00:02:20,429 --> 00:02:21,705 make a winning move. 41 00:02:21,705 --> 00:02:22,691 Before you move on, 42 00:02:22,691 --> 00:02:26,177 I would like to talk to you about the commenting you see above my method. 43 00:02:26,177 --> 00:02:29,557 This is a commonly used way to document JavaScript code. 44 00:02:29,557 --> 00:02:32,695 Every method in your classes should be documented like this. 45 00:02:32,695 --> 00:02:36,797 This way, anyone, including yourself if it's been a while since you've worked 46 00:02:36,797 --> 00:02:40,733 with a code base, can quickly figure out what each method is responsible for. 47 00:02:40,733 --> 00:02:44,269 The documentation should give a quick summary of the method, and 48 00:02:44,269 --> 00:02:48,396 then describe any arguments it receives, and anything that's returned. 49 00:02:48,396 --> 00:02:53,186 Check out the teacher's notes for a link to a website that describes the syntax you 50 00:02:53,186 --> 00:02:55,904 should use for this documentation in detail. 51 00:02:55,904 --> 00:02:59,451 Okay, take a moment to make any necessary changes to your code so 52 00:02:59,451 --> 00:03:02,220 that it matches the solution you see on-screen. 53 00:03:02,220 --> 00:03:06,473 And then head over to the next step, where you will start building out the board.