Afrikaans
Akan
Albanian
Amharic
Arabic
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
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, here goes coding challenge number two,
which is gonna be all about arrays.
And in this one,
we're gonna go back to the tip calculator
that we built in the previous section.
And we're still using the same rules as before.
So we tip 15% of the bill value, if it's between 50 and 300.
And if the value is different, so outside of this range,
the tip is 20%.
And so here are your tasks.
First, write a function called calcTip,
which takes any bill value as an input,
and returns the corresponding tip.
And so that's gonna be based on the rules
that I just mentioned.
And to do this, you can actually check out the code
from the first tip calculator challenge
that we did in the last section.
So you can copy the code there.
Or if you want, you can also code this from scratch,
just to remember how to do it.
And now in number two, we're finally gonna use arrays.
So here, we're gonna create an array called bills,
which will contain the test data that we have down here.
So these are the three bill values
for which we want to calculate the tips.
And so that's what we do in point number three.
So here, we create an array called tips,
which will contain the tip value
for each of the three bills.
And this is where we're gonna use the calcTip function
that we created before.
So that's the basic challenge.
And if you wanna take it one step further,
you can even take the bonus question.
So number four here,
in which I ask you to create an array called total,
which will have the total values.
So that's the bill plus the tip.
So I hope this one isn't too hard.
Basically, it's similar to one of the examples actually,
that we did in one of the lectures about the arrays.
And once again,
I also have a hint to help you solving this one.
But if you want to try it on your own without the hint
then pause video now, and good luck.
But if you want to hint,
well, then here it goes.
So remember that an array needs a value in each position.
But that value
can actually be the returned value of a function.
So as an array value,
you can basically just call a function. Alright.
And again, that is similar
to something that we did in one of the array lectures.
So pause the video now,
and I see you with my solution in a second.
Okay, and I'm gonna start by creating the calcTip function.
And I'm gonna start doing it with a function expression.
So this one will receive a bill as an input,
I believe, I think,
yeah, so calcTip takes any bill as an input.
So let's simply call it bill.
And then what we want to return is the tip.
And here I'm gonna write the exact same code
as we did in the other challenge.
So there's no need to explain it all again,
it would just be a waste of time.
So if the bill value is between 50 and 300,
so less or equal than 300.
And here we're using the ternary operator,
then return the bill value, times zero point 15.
So that's 15%.
And otherwise, it should be 20%.
Okay, so the result of this ternary operator here
will be one of these values.
So either bill times point 15 or times point two.
And so that's gonna be the result of this operator.
And so that's what this function will return.
If you prefer the arrow function, we can also do that.
So const calcTip equal bill.
And now we can omit the return keyword
and do it all in one nice line.
So now you just have to choose which one you like better.
And I will just for the sake of readability.
Keep the first one there. Okay.
But if you prefer the arrow function,
you can just use this one. Great.
So number two is to simply create a bills array
with these three values.
So bills 125, five five five and 44.
So that was easy.
And now we will create an array tips,
which will contain the tip value for each bill.
So, let's do that.
And now this is similar to what we did previously
with calculating the ages.
So we are going to create an array,
and then each position will be the function call to calcTip.
So we want to calc the tip with bills at position zero.
Then at position one, we want to calculate bills.
Or we actually want to calculate tips based on bills
on position one, and then on position two. And that's it.
So let's log both the bills
and the tips to the console here.
And that gives us tips is not defined.
So here, it should be tips.
So usually when I have an array,
I like to give it a plural name,
like here bills and here tips.
So because it's multiple values,
so now that should work.
And yeah, so these were the three tips that were calculated.
And of course, you could have created separate values,
or actually separate variables
for each of these function calls,
like tip one equals calcTip with bills, zero,
something like this.
And then tip two and tip three.
And then you could have created
like an array with tip one, tip two and tip three.
Okay, so something like this.
But my idea was to actually do it like this,
because then we don't have to create
all these extra variables. So that's a lot cleaner.
Later on in the section,
we will find an even better way of doing this.
But with what we know, at this point,
this is the best that we can do.
So we take the first bill,
calculate the tip based on that
and put it in position number zero,
then we take bills at position number one
calculate the tip and put it in position one
of this new array,
and then we take bills at position number two,
calculate the tip
and put it at position two of the new array.
And so then, now we end up with this.
Lastly, let's also complete the bonus question
number four here.
And all you have to do here is to create an array,
which will have the sum of the bills and the tips.
So that's gonna be totals.
And now you might have tried to do something like this.
So like bills plus tips.
But as I told you in the previous lecture,
this is simply not gonna work.
So we cannot do any operations on arrays like this,
it doesn't work like that.
So let me show you the result that we would get
from doing this.
And so basically, each of the arrays would be transformed
to a string, and then the two strings would be concatenated.
So that's completely useless.
And so the only thing that we could do
is to say that we want bills at position zero,
plus the tips at position zero.
And now we do this for all the three values.
So at position one as well.
And then at position two.
And so this fixes this error, because now in each position,
we do actually have a real value,
because this is an actual value.
And so with that, we can then do calculations.
So, yeah, this is correct. Looks correct. Right.
And yeah, so with that, all of the four points are solved.
And so here we conclude this part about arrays
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.