Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,320 --> 00:00:00,630
All right.
2
00:00:00,630 --> 00:00:07,440
So this was a relatively simple code challenge and the goal is very simple given that we've got a function
3
00:00:07,440 --> 00:00:13,480
called who's paying and we pass in an array of names like this one.
4
00:00:13,650 --> 00:00:20,790
Our function should be able to pick out a random name from this array and be able to return the output
5
00:00:21,120 --> 00:00:24,250
that one of these people is going to buy lunch today.
6
00:00:24,570 --> 00:00:26,980
So let's see how we might tackle this.
7
00:00:27,030 --> 00:00:34,230
Now in order to pick out an item from the array we would tap into the array and we would use a square
8
00:00:34,230 --> 00:00:36,630
bracket to select an item.
9
00:00:36,630 --> 00:00:42,570
So if we said square brackets zero then that's going to pull out the item at position 0 which is the
10
00:00:42,570 --> 00:00:43,510
first one.
11
00:00:43,680 --> 00:00:52,740
And if we did names at position for then that's going to be 0 1 2 3 4 and it's going to pull out the
12
00:00:52,740 --> 00:00:55,140
last item from the array.
13
00:00:55,140 --> 00:01:04,320
So now we basically need a random number that is in the range of 0 to 4 if the input was this long.
14
00:01:04,320 --> 00:01:07,400
But we don't know how long the input could be right.
15
00:01:07,410 --> 00:01:09,980
It could be an array of names as long as my arm.
16
00:01:09,990 --> 00:01:13,730
So given that we don't know that information let's first work that out.
17
00:01:13,890 --> 00:01:19,230
Let's create a variable called number of people which is going to be equal to names dot length.
18
00:01:19,800 --> 00:01:26,360
And this is going to calculate in this array that we got past how many names are there right.
19
00:01:26,460 --> 00:01:33,040
Because once we've got that information then we can work out a random person's position.
20
00:01:33,060 --> 00:01:33,390
Right.
21
00:01:33,720 --> 00:01:39,960
So we could say that the random person position is equal to math at random which is going to generate
22
00:01:40,020 --> 00:01:44,110
a number between 0 and 1 and not including 1.
23
00:01:44,160 --> 00:01:50,730
And then we can multiply it by the number of people then we would get a range of numbers from zero to
24
00:01:50,730 --> 00:01:53,520
nought point 9 9 9 9 9 recurring.
25
00:01:53,520 --> 00:01:58,550
So now if we multiply it by the total number of peoples the number of items in this array.
26
00:01:58,560 --> 00:02:01,400
So in this example we would have five.
27
00:02:01,530 --> 00:02:07,350
So zero multiplied by five is going to be zero but nought point nine nine nine nine nine multiplied
28
00:02:07,350 --> 00:02:11,970
by five is going to become four point nine 0 9 0 9.
29
00:02:11,970 --> 00:02:20,280
So now all we have to do is use the mass dot floor to cut off everything that's at the end of the decimal
30
00:02:20,280 --> 00:02:23,550
place and then we get our range 0 to 4.
31
00:02:23,550 --> 00:02:25,050
So let's go ahead and do that.
32
00:02:25,740 --> 00:02:33,570
So I'm going to wrap everything inside a math dot floor and now and now this random person position
33
00:02:33,870 --> 00:02:41,310
should be a random number between zero and the total number of people minus one giving us all of the
34
00:02:41,310 --> 00:02:44,250
indices that we need to get each item in the array.
35
00:02:44,910 --> 00:02:51,450
So now all that's to do is to pick out the random person and to do that we tap into the list of names
36
00:02:51,630 --> 00:02:56,440
use our square brackets and pass in the random person position.
37
00:02:56,730 --> 00:03:01,200
Now that we've got our random person all that we have to do is provide this output.
38
00:03:01,740 --> 00:03:04,170
So let's return this output.
39
00:03:04,380 --> 00:03:14,670
And we're going to return the random person plus the string is going to buy lunch today.
40
00:03:14,670 --> 00:03:16,140
Exclamation mark.
41
00:03:16,140 --> 00:03:21,470
Now remember to add the space between the random person's name and the rest of the sentence.
42
00:03:21,540 --> 00:03:27,870
And now we've completed the challenge and we're ready to check our solution and all going well it should
43
00:03:27,870 --> 00:03:28,740
work.
44
00:03:28,740 --> 00:03:34,020
And if you run this through replayed or through the Chrome's inputs too you should be able to see that
45
00:03:34,140 --> 00:03:39,450
each time you run this code and you pass in a different array or the same array it should give you a
46
00:03:39,450 --> 00:03:41,250
random name from that list.
4683
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.