WEBVTT 0 00:00.300 --> 00:06.690 All right, so this was a relatively simple code challenge, and the goal is very simple. Given that we've 1 00:06.690 --> 00:12.900 got a function called whosPaying and we pass in an array of names like this one, 2 00:13.560 --> 00:20.820 our function should be able to pick out a random name from this array and be able to return the output 3 00:21.090 --> 00:24.250 that one of these people is going to buy lunch today. 4 00:24.510 --> 00:30.990 So let's see how we might tackle this. Now in order to pick out an item from the array, we would tap 5 00:30.990 --> 00:36.270 into the array and we would use a square bracket to select an item. 6 00:36.570 --> 00:42.360 So if we said [0], then that's going to pull out the item at position zero which 7 00:42.360 --> 00:43.260 is the first one. 8 00:43.590 --> 00:49.880 And if we did names [4], then that's going to be 0, 9 00:49.890 --> 00:54.480 1, 2, 3, 4, and it's going to pull up the last item from the array. 10 00:55.110 --> 01:03.600 So now we basically need a random number that is in the range of 0 to 4 if the input was this 11 01:03.600 --> 01:04.020 long. 12 01:04.320 --> 01:06.930 But we don't know how long the input could be, 13 01:06.930 --> 01:07.300 right? 14 01:07.320 --> 01:09.660 It could be an array of names as long as my arm. 15 01:09.930 --> 01:13.530 So given that we don't know that information, let's first work that out. 16 01:13.860 --> 01:19.230 Let's create a variable called numberOfPeople which is going to be equal to names.length. 17 01:19.710 --> 01:25.380 And this is going to calculate in this array that we got past how many names are there, 18 01:25.500 --> 01:25.860 right? 19 01:26.460 --> 01:33.000 Because once we've got that information, then we can work out a random person's position, 20 01:33.000 --> 01:33.410 right? 21 01:33.690 --> 01:39.990 So we could say that the randomPersonPosition = math.random, which is going to generate 22 01:39.990 --> 01:43.710 a number between 0 and 1 and not including 1. 23 01:44.040 --> 01:47.360 And then we can multiply it by the numberOfPeople 24 01:48.180 --> 01:52.950 then we would get a range of numbers from 0 to 0.9999 recurring. 25 01:53.490 --> 01:58.360 So now if we multiply it by the total number of people, so the number of items in this array, 26 01:58.530 --> 02:01.140 so in this example, we would have five. 27 02:01.500 --> 02:08.760 So zero multiplied by five is going to be zero, but 0.999 multiplied by five is going to 28 02:08.760 --> 02:11.150 become 4.9999. 29 02:11.940 --> 02:20.310 So now all we have to do is use the math.floor to cut off everything that's at the end of the decimal 30 02:20.310 --> 02:20.700 place 31 02:20.940 --> 02:23.000 and then we get our range 0 to 4. 32 02:23.520 --> 02:25.050 So let's go ahead and do that. 33 02:25.680 --> 02:28.800 So I'm going to wrap everything inside a math.floor 34 02:29.370 --> 02:38.640 and now this randomPersonPosition should be a random number between 0 and the total number 35 02:38.640 --> 02:44.220 OfPeople - 1, giving us all of the indices that we need to get each item in the array. 36 02:44.880 --> 02:48.540 So now all that's to do is to pick out the random person. 37 02:48.990 --> 02:55.980 And to do that, we tap into the list of names, use our square brackets and pass in the randomPerson position. 38 02:56.700 --> 03:01.190 Now that we've got our random person, all that we have to do is provide this output. 39 03:01.680 --> 03:06.810 So let's return this output and we're going to return the random person 40 03:07.140 --> 03:13.950 plus the string "is going to buy lunch today." 41 03:14.580 --> 03:15.640 ! 42 03:16.080 --> 03:21.080 Now, remember to add the space between the random person's name and the rest of the sentence. 43 03:21.450 --> 03:27.660 And now we've completed the challenge and we're ready to check our solution and all going well, it 44 03:27.660 --> 03:28.250 should work. 45 03:28.650 --> 03:34.410 And if you run this through Repl.it or through the Chrome snippets tool, you should be able to see that each 46 03:34.410 --> 03:40.020 time you run this code and you pass in a different array or the same array, it should give you a random 47 03:40.020 --> 03:41.250 name from that list.