Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:01,390 --> 00:00:05,320
All right, here goes coding challenge number two,
2
2
00:00:05,320 --> 00:00:07,693
which is gonna be all about arrays.
3
3
00:00:09,420 --> 00:00:10,320
And in this one,
4
4
00:00:10,320 --> 00:00:12,910
we're gonna go back to the tip calculator
5
5
00:00:12,910 --> 00:00:15,540
that we built in the previous section.
6
6
00:00:15,540 --> 00:00:18,140
And we're still using the same rules as before.
7
7
00:00:18,140 --> 00:00:23,140
So we tip 15% of the bill value, if it's between 50 and 300.
8
8
00:00:24,070 --> 00:00:28,050
And if the value is different, so outside of this range,
9
9
00:00:28,050 --> 00:00:30,691
the tip is 20%.
10
10
00:00:30,691 --> 00:00:32,780
And so here are your tasks.
11
11
00:00:32,780 --> 00:00:35,580
First, write a function called calcTip,
12
12
00:00:35,580 --> 00:00:38,850
which takes any bill value as an input,
13
13
00:00:38,850 --> 00:00:40,970
and returns the corresponding tip.
14
14
00:00:40,970 --> 00:00:43,320
And so that's gonna be based on the rules
15
15
00:00:43,320 --> 00:00:45,060
that I just mentioned.
16
16
00:00:45,060 --> 00:00:47,520
And to do this, you can actually check out the code
17
17
00:00:47,520 --> 00:00:49,860
from the first tip calculator challenge
18
18
00:00:49,860 --> 00:00:51,670
that we did in the last section.
19
19
00:00:51,670 --> 00:00:53,350
So you can copy the code there.
20
20
00:00:53,350 --> 00:00:56,540
Or if you want, you can also code this from scratch,
21
21
00:00:56,540 --> 00:00:58,870
just to remember how to do it.
22
22
00:00:58,870 --> 00:01:03,170
And now in number two, we're finally gonna use arrays.
23
23
00:01:03,170 --> 00:01:06,260
So here, we're gonna create an array called bills,
24
24
00:01:06,260 --> 00:01:11,260
which will contain the test data that we have down here.
25
25
00:01:11,300 --> 00:01:13,710
So these are the three bill values
26
26
00:01:13,710 --> 00:01:16,210
for which we want to calculate the tips.
27
27
00:01:16,210 --> 00:01:19,810
And so that's what we do in point number three.
28
28
00:01:19,810 --> 00:01:22,450
So here, we create an array called tips,
29
29
00:01:22,450 --> 00:01:24,500
which will contain the tip value
30
30
00:01:24,500 --> 00:01:26,757
for each of the three bills.
31
31
00:01:26,757 --> 00:01:30,119
And this is where we're gonna use the calcTip function
32
32
00:01:30,119 --> 00:01:32,040
that we created before.
33
33
00:01:32,040 --> 00:01:33,790
So that's the basic challenge.
34
34
00:01:33,790 --> 00:01:36,210
And if you wanna take it one step further,
35
35
00:01:36,210 --> 00:01:39,220
you can even take the bonus question.
36
36
00:01:39,220 --> 00:01:40,730
So number four here,
37
37
00:01:40,730 --> 00:01:44,580
in which I ask you to create an array called total,
38
38
00:01:44,580 --> 00:01:47,390
which will have the total values.
39
39
00:01:47,390 --> 00:01:50,520
So that's the bill plus the tip.
40
40
00:01:50,520 --> 00:01:53,100
So I hope this one isn't too hard.
41
41
00:01:53,100 --> 00:01:56,250
Basically, it's similar to one of the examples actually,
42
42
00:01:56,250 --> 00:01:59,900
that we did in one of the lectures about the arrays.
43
43
00:01:59,900 --> 00:02:00,750
And once again,
44
44
00:02:00,750 --> 00:02:04,080
I also have a hint to help you solving this one.
45
45
00:02:04,080 --> 00:02:07,250
But if you want to try it on your own without the hint
46
46
00:02:07,250 --> 00:02:09,873
then pause video now, and good luck.
47
47
00:02:11,220 --> 00:02:13,210
But if you want to hint,
48
48
00:02:13,210 --> 00:02:15,730
well, then here it goes.
49
49
00:02:15,730 --> 00:02:20,220
So remember that an array needs a value in each position.
50
50
00:02:20,220 --> 00:02:21,053
But that value
51
51
00:02:21,053 --> 00:02:24,710
can actually be the returned value of a function.
52
52
00:02:24,710 --> 00:02:26,070
So as an array value,
53
53
00:02:26,070 --> 00:02:29,860
you can basically just call a function. Alright.
54
54
00:02:29,860 --> 00:02:31,680
And again, that is similar
55
55
00:02:31,680 --> 00:02:35,860
to something that we did in one of the array lectures.
56
56
00:02:35,860 --> 00:02:37,360
So pause the video now,
57
57
00:02:37,360 --> 00:02:40,003
and I see you with my solution in a second.
58
58
00:02:42,840 --> 00:02:47,003
Okay, and I'm gonna start by creating the calcTip function.
59
59
00:02:52,860 --> 00:02:57,050
And I'm gonna start doing it with a function expression.
60
60
00:02:57,050 --> 00:03:00,293
So this one will receive a bill as an input,
61
61
00:03:01,510 --> 00:03:02,803
I believe, I think,
62
62
00:03:03,890 --> 00:03:07,490
yeah, so calcTip takes any bill as an input.
63
63
00:03:07,490 --> 00:03:09,360
So let's simply call it bill.
64
64
00:03:09,360 --> 00:03:13,120
And then what we want to return is the tip.
65
65
00:03:13,120 --> 00:03:15,310
And here I'm gonna write the exact same code
66
66
00:03:15,310 --> 00:03:17,280
as we did in the other challenge.
67
67
00:03:17,280 --> 00:03:19,723
So there's no need to explain it all again,
68
68
00:03:20,590 --> 00:03:22,720
it would just be a waste of time.
69
69
00:03:22,720 --> 00:03:27,720
So if the bill value is between 50 and 300,
70
70
00:03:29,520 --> 00:03:33,800
so less or equal than 300.
71
71
00:03:33,800 --> 00:03:36,720
And here we're using the ternary operator,
72
72
00:03:36,720 --> 00:03:41,720
then return the bill value, times zero point 15.
73
73
00:03:41,800 --> 00:03:43,470
So that's 15%.
74
74
00:03:43,470 --> 00:03:47,743
And otherwise, it should be 20%.
75
75
00:03:49,790 --> 00:03:53,910
Okay, so the result of this ternary operator here
76
76
00:03:53,910 --> 00:03:56,520
will be one of these values.
77
77
00:03:56,520 --> 00:04:01,030
So either bill times point 15 or times point two.
78
78
00:04:01,030 --> 00:04:04,260
And so that's gonna be the result of this operator.
79
79
00:04:04,260 --> 00:04:06,733
And so that's what this function will return.
80
80
00:04:08,560 --> 00:04:12,280
If you prefer the arrow function, we can also do that.
81
81
00:04:12,280 --> 00:04:17,280
So const calcTip equal bill.
82
82
00:04:19,500 --> 00:04:22,330
And now we can omit the return keyword
83
83
00:04:22,330 --> 00:04:24,603
and do it all in one nice line.
84
84
00:04:25,890 --> 00:04:29,860
So now you just have to choose which one you like better.
85
85
00:04:29,860 --> 00:04:32,683
And I will just for the sake of readability.
86
86
00:04:33,640 --> 00:04:36,100
Keep the first one there. Okay.
87
87
00:04:36,100 --> 00:04:37,890
But if you prefer the arrow function,
88
88
00:04:37,890 --> 00:04:41,260
you can just use this one. Great.
89
89
00:04:41,260 --> 00:04:45,530
So number two is to simply create a bills array
90
90
00:04:45,530 --> 00:04:47,333
with these three values.
91
91
00:04:50,610 --> 00:04:55,610
So bills 125, five five five and 44.
92
92
00:04:57,890 --> 00:04:59,760
So that was easy.
93
93
00:04:59,760 --> 00:05:04,150
And now we will create an array tips,
94
94
00:05:04,150 --> 00:05:06,963
which will contain the tip value for each bill.
95
95
00:05:09,150 --> 00:05:11,203
So, let's do that.
96
96
00:05:12,110 --> 00:05:14,410
And now this is similar to what we did previously
97
97
00:05:14,410 --> 00:05:16,490
with calculating the ages.
98
98
00:05:16,490 --> 00:05:19,100
So we are going to create an array,
99
99
00:05:19,100 --> 00:05:23,470
and then each position will be the function call to calcTip.
100
100
00:05:24,760 --> 00:05:29,760
So we want to calc the tip with bills at position zero.
101
101
00:05:31,310 --> 00:05:35,233
Then at position one, we want to calculate bills.
102
102
00:05:36,410 --> 00:05:39,550
Or we actually want to calculate tips based on bills
103
103
00:05:39,550 --> 00:05:44,550
on position one, and then on position two. And that's it.
104
104
00:05:48,780 --> 00:05:50,820
So let's log both the bills
105
105
00:05:52,560 --> 00:05:54,733
and the tips to the console here.
106
106
00:05:57,960 --> 00:06:01,060
And that gives us tips is not defined.
107
107
00:06:01,060 --> 00:06:02,750
So here, it should be tips.
108
108
00:06:02,750 --> 00:06:05,260
So usually when I have an array,
109
109
00:06:05,260 --> 00:06:07,230
I like to give it a plural name,
110
110
00:06:07,230 --> 00:06:10,980
like here bills and here tips.
111
111
00:06:10,980 --> 00:06:12,983
So because it's multiple values,
112
112
00:06:14,400 --> 00:06:16,920
so now that should work.
113
113
00:06:16,920 --> 00:06:21,850
And yeah, so these were the three tips that were calculated.
114
114
00:06:21,850 --> 00:06:24,950
And of course, you could have created separate values,
115
115
00:06:24,950 --> 00:06:26,980
or actually separate variables
116
116
00:06:26,980 --> 00:06:28,970
for each of these function calls,
117
117
00:06:28,970 --> 00:06:33,970
like tip one equals calcTip with bills, zero,
118
118
00:06:35,560 --> 00:06:36,620
something like this.
119
119
00:06:36,620 --> 00:06:38,530
And then tip two and tip three.
120
120
00:06:38,530 --> 00:06:41,180
And then you could have created
121
121
00:06:41,180 --> 00:06:45,753
like an array with tip one, tip two and tip three.
122
122
00:06:47,330 --> 00:06:49,280
Okay, so something like this.
123
123
00:06:49,280 --> 00:06:53,070
But my idea was to actually do it like this,
124
124
00:06:53,070 --> 00:06:54,620
because then we don't have to create
125
125
00:06:54,620 --> 00:06:58,083
all these extra variables. So that's a lot cleaner.
126
126
00:07:00,380 --> 00:07:01,810
Later on in the section,
127
127
00:07:01,810 --> 00:07:04,840
we will find an even better way of doing this.
128
128
00:07:04,840 --> 00:07:07,110
But with what we know, at this point,
129
129
00:07:07,110 --> 00:07:09,033
this is the best that we can do.
130
130
00:07:10,030 --> 00:07:12,330
So we take the first bill,
131
131
00:07:12,330 --> 00:07:14,700
calculate the tip based on that
132
132
00:07:14,700 --> 00:07:18,280
and put it in position number zero,
133
133
00:07:18,280 --> 00:07:21,710
then we take bills at position number one
134
134
00:07:21,710 --> 00:07:24,420
calculate the tip and put it in position one
135
135
00:07:24,420 --> 00:07:25,790
of this new array,
136
136
00:07:25,790 --> 00:07:29,450
and then we take bills at position number two,
137
137
00:07:29,450 --> 00:07:30,590
calculate the tip
138
138
00:07:30,590 --> 00:07:33,773
and put it at position two of the new array.
139
139
00:07:35,270 --> 00:07:39,043
And so then, now we end up with this.
140
140
00:07:41,810 --> 00:07:44,970
Lastly, let's also complete the bonus question
141
141
00:07:44,970 --> 00:07:46,540
number four here.
142
142
00:07:46,540 --> 00:07:49,870
And all you have to do here is to create an array,
143
143
00:07:49,870 --> 00:07:53,573
which will have the sum of the bills and the tips.
144
144
00:07:55,400 --> 00:07:57,663
So that's gonna be totals.
145
145
00:07:59,260 --> 00:08:03,670
And now you might have tried to do something like this.
146
146
00:08:03,670 --> 00:08:07,430
So like bills plus tips.
147
147
00:08:07,430 --> 00:08:11,260
But as I told you in the previous lecture,
148
148
00:08:11,260 --> 00:08:13,100
this is simply not gonna work.
149
149
00:08:13,100 --> 00:08:16,640
So we cannot do any operations on arrays like this,
150
150
00:08:16,640 --> 00:08:18,033
it doesn't work like that.
151
151
00:08:19,520 --> 00:08:22,020
So let me show you the result that we would get
152
152
00:08:22,020 --> 00:08:23,073
from doing this.
153
153
00:08:25,040 --> 00:08:28,120
And so basically, each of the arrays would be transformed
154
154
00:08:28,120 --> 00:08:31,843
to a string, and then the two strings would be concatenated.
155
155
00:08:33,490 --> 00:08:36,380
So that's completely useless.
156
156
00:08:36,380 --> 00:08:39,260
And so the only thing that we could do
157
157
00:08:39,260 --> 00:08:44,260
is to say that we want bills at position zero,
158
158
00:08:44,370 --> 00:08:48,453
plus the tips at position zero.
159
159
00:08:50,480 --> 00:08:55,173
And now we do this for all the three values.
160
160
00:08:57,660 --> 00:08:59,670
So at position one as well.
161
161
00:08:59,670 --> 00:09:03,813
And then at position two.
162
162
00:09:04,690 --> 00:09:08,700
And so this fixes this error, because now in each position,
163
163
00:09:08,700 --> 00:09:10,990
we do actually have a real value,
164
164
00:09:10,990 --> 00:09:13,460
because this is an actual value.
165
165
00:09:13,460 --> 00:09:16,653
And so with that, we can then do calculations.
166
166
00:09:18,400 --> 00:09:23,400
So, yeah, this is correct. Looks correct. Right.
167
167
00:09:25,740 --> 00:09:30,340
And yeah, so with that, all of the four points are solved.
168
168
00:09:30,340 --> 00:09:33,540
And so here we conclude this part about arrays
14464
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.