Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right.
So this was a relatively simple code challenge and the goal is very simple given that we've got a function
called who's paying and we pass in an array of names like this one.
Our function should be able to pick out a random name from this array and be able to return the output
that one of these people is going to buy lunch today.
So let's see how we might tackle this.
Now in order to pick out an item from the array we would tap into the array and we would use a square
bracket to select an item.
So if we said square brackets zero then that's going to pull out the item at position 0 which is the
first one.
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
last item from the array.
So now we basically need a random number that is in the range of 0 to 4 if the input was this long.
But we don't know how long the input could be right.
It could be an array of names as long as my arm.
So given that we don't know that information let's first work that out.
Let's create a variable called number of people which is going to be equal to names dot length.
And this is going to calculate in this array that we got past how many names are there right.
Because once we've got that information then we can work out a random person's position.
Right.
So we could say that the random person position is equal to math at random which is going to generate
a number between 0 and 1 and not including 1.
And then we can multiply it by the number of people then we would get a range of numbers from zero to
nought point 9 9 9 9 9 recurring.
So now if we multiply it by the total number of peoples the number of items in this array.
So in this example we would have five.
So zero multiplied by five is going to be zero but nought point nine nine nine nine nine multiplied
by five is going to become four point nine 0 9 0 9.
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
place and then we get our range 0 to 4.
So let's go ahead and do that.
So I'm going to wrap everything inside a math dot floor and now and now this random person position
should be a random number between zero and the total number of people minus one giving us all of the
indices that we need to get each item in the array.
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
use our square brackets and pass in the random person position.
Now that we've got our random person all that we have to do is provide this output.
So let's return this output.
And we're going to return the random person plus the string is going to buy lunch today.
Exclamation mark.
Now remember to add the space between the random person's name and the rest of the sentence.
And now we've completed the challenge and we're ready to check our solution and all going well it should
work.
And if you run this through replayed or through the Chrome's inputs too you should be able to see that
each time you run this code and you pass in a different array or the same array it should give you a
random name from that list.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.