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
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
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
1 1
All right, here goes coding challenge number two, 2
2
which is gonna be all about arrays. 3
3
And in this one, 4
4
we're gonna go back to the tip calculator 5
5
that we built in the previous section. 6
6
And we're still using the same rules as before. 7
7
So we tip 15% of the bill value, if it's between 50 and 300. 8
8
And if the value is different, so outside of this range, 9
9
the tip is 20%. 10
10
And so here are your tasks. 11
11
First, write a function called calcTip, 12
12
which takes any bill value as an input, 13
13
and returns the corresponding tip. 14
14
And so that's gonna be based on the rules 15
15
that I just mentioned. 16
16
And to do this, you can actually check out the code 17
17
from the first tip calculator challenge 18
18
that we did in the last section. 19
19
So you can copy the code there. 20
20
Or if you want, you can also code this from scratch, 21
21
just to remember how to do it. 22
22
And now in number two, we're finally gonna use arrays. 23
23
So here, we're gonna create an array called bills, 24
24
which will contain the test data that we have down here. 25
25
So these are the three bill values 26
26
for which we want to calculate the tips. 27
27
And so that's what we do in point number three. 28
28
So here, we create an array called tips, 29
29
which will contain the tip value 30
30
for each of the three bills. 31
31
And this is where we're gonna use the calcTip function 32
32
that we created before. 33
33
So that's the basic challenge. 34
34
And if you wanna take it one step further, 35
35
you can even take the bonus question. 36
36
So number four here, 37
37
in which I ask you to create an array called total, 38
38
which will have the total values. 39
39
So that's the bill plus the tip. 40
40
So I hope this one isn't too hard. 41
41
Basically, it's similar to one of the examples actually, 42
42
that we did in one of the lectures about the arrays. 43
43
And once again, 44
44
I also have a hint to help you solving this one. 45
45
But if you want to try it on your own without the hint 46
46
then pause video now, and good luck. 47
47
But if you want to hint, 48
48
well, then here it goes. 49
49
So remember that an array needs a value in each position. 50
50
But that value 51
51
can actually be the returned value of a function. 52
52
So as an array value, 53
53
you can basically just call a function. Alright. 54
54
And again, that is similar 55
55
to something that we did in one of the array lectures. 56
56
So pause the video now, 57
57
and I see you with my solution in a second. 58
58
Okay, and I'm gonna start by creating the calcTip function. 59
59
And I'm gonna start doing it with a function expression. 60
60
So this one will receive a bill as an input, 61
61
I believe, I think, 62
62
yeah, so calcTip takes any bill as an input. 63
63
So let's simply call it bill. 64
64
And then what we want to return is the tip. 65
65
And here I'm gonna write the exact same code 66
66
as we did in the other challenge. 67
67
So there's no need to explain it all again, 68
68
it would just be a waste of time. 69
69
So if the bill value is between 50 and 300, 70
70
so less or equal than 300. 71
71
And here we're using the ternary operator, 72
72
then return the bill value, times zero point 15. 73
73
So that's 15%. 74
74
And otherwise, it should be 20%. 75
75
Okay, so the result of this ternary operator here 76
76
will be one of these values. 77
77
So either bill times point 15 or times point two. 78
78
And so that's gonna be the result of this operator. 79
79
And so that's what this function will return. 80
80
If you prefer the arrow function, we can also do that. 81
81
So const calcTip equal bill. 82
82
And now we can omit the return keyword 83
83
and do it all in one nice line. 84
84
So now you just have to choose which one you like better. 85
85
And I will just for the sake of readability. 86
86
Keep the first one there. Okay. 87
87
But if you prefer the arrow function, 88
88
you can just use this one. Great. 89
89
So number two is to simply create a bills array 90
90
with these three values. 91
91
So bills 125, five five five and 44. 92
92
So that was easy. 93
93
And now we will create an array tips, 94
94
which will contain the tip value for each bill. 95
95
So, let's do that. 96
96
And now this is similar to what we did previously 97
97
with calculating the ages. 98
98
So we are going to create an array, 99
99
and then each position will be the function call to calcTip. 100
100
So we want to calc the tip with bills at position zero. 101
101
Then at position one, we want to calculate bills. 102
102
Or we actually want to calculate tips based on bills 103
103
on position one, and then on position two. And that's it. 104
104
So let's log both the bills 105
105
and the tips to the console here. 106
106
And that gives us tips is not defined. 107
107
So here, it should be tips. 108
108
So usually when I have an array, 109
109
I like to give it a plural name, 110
110
like here bills and here tips. 111
111
So because it's multiple values, 112
112
so now that should work. 113
113
And yeah, so these were the three tips that were calculated. 114
114
And of course, you could have created separate values, 115
115
or actually separate variables 116
116
for each of these function calls, 117
117
like tip one equals calcTip with bills, zero, 118
118
something like this. 119
119
And then tip two and tip three. 120
120
And then you could have created 121
121
like an array with tip one, tip two and tip three. 122
122
Okay, so something like this. 123
123
But my idea was to actually do it like this, 124
124
because then we don't have to create 125
125
all these extra variables. So that's a lot cleaner. 126
126
Later on in the section, 127
127
we will find an even better way of doing this. 128
128
But with what we know, at this point, 129
129
this is the best that we can do. 130
130
So we take the first bill, 131
131
calculate the tip based on that 132
132
and put it in position number zero, 133
133
then we take bills at position number one 134
134
calculate the tip and put it in position one 135
135
of this new array, 136
136
and then we take bills at position number two, 137
137
calculate the tip 138
138
and put it at position two of the new array. 139
139
And so then, now we end up with this. 140
140
Lastly, let's also complete the bonus question 141
141
number four here. 142
142
And all you have to do here is to create an array, 143
143
which will have the sum of the bills and the tips. 144
144
So that's gonna be totals. 145
145
And now you might have tried to do something like this. 146
146
So like bills plus tips. 147
147
But as I told you in the previous lecture, 148
148
this is simply not gonna work. 149
149
So we cannot do any operations on arrays like this, 150
150
it doesn't work like that. 151
151
So let me show you the result that we would get 152
152
from doing this. 153
153
And so basically, each of the arrays would be transformed 154
154
to a string, and then the two strings would be concatenated. 155
155
So that's completely useless. 156
156
And so the only thing that we could do 157
157
is to say that we want bills at position zero, 158
158
plus the tips at position zero. 159
159
And now we do this for all the three values. 160
160
So at position one as well. 161
161
And then at position two. 162
162
And so this fixes this error, because now in each position, 163
163
we do actually have a real value, 164
164
because this is an actual value. 165
165
And so with that, we can then do calculations. 166
166
So, yeah, this is correct. Looks correct. Right. 167
167
And yeah, so with that, all of the four points are solved. 168
168
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.