Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,260 --> 00:00:04,040
It's now time for your final coding challenge
2
00:00:04,040 --> 00:00:05,620
in this section.
3
00:00:05,620 --> 00:00:07,820
And this one is gonna allow you to assess
4
00:00:07,820 --> 00:00:09,570
whether you actually understood
5
00:00:09,570 --> 00:00:11,760
how the ternary operator works.
6
00:00:11,760 --> 00:00:15,200
So it's gonna be really fun and actually really short.
7
00:00:15,200 --> 00:00:16,993
So let's do that now.
8
00:00:18,610 --> 00:00:20,060
And here it goes,
9
00:00:20,060 --> 00:00:23,930
so Steven wants to build a very simple tip calculator
10
00:00:23,930 --> 00:00:27,240
for whenever he goes eating in a restaurant.
11
00:00:27,240 --> 00:00:31,040
Now in his country, it's usual to tip 15%
12
00:00:31,040 --> 00:00:35,290
if the bill value is between 50 and 300.
13
00:00:35,290 --> 00:00:37,010
And if the value is different,
14
00:00:37,010 --> 00:00:41,170
so if it's not between this range between 50 and 300,
15
00:00:41,170 --> 00:00:43,920
then the tip is usually 20%.
16
00:00:43,920 --> 00:00:46,320
So your task is to calculate the tip
17
00:00:46,320 --> 00:00:48,270
depending on the bill value,
18
00:00:48,270 --> 00:00:51,450
according to the rules that I just mentioned,
19
00:00:51,450 --> 00:00:55,230
and to do this, you will create a variable called tip,
20
00:00:55,230 --> 00:00:57,970
which is then gonna store the tip value.
21
00:00:57,970 --> 00:01:00,970
And probably you should also create a bill variable
22
00:01:00,970 --> 00:01:03,590
which just gonna hold the test data.
23
00:01:03,590 --> 00:01:07,637
So first 275, 1040, and then 430.
24
00:01:08,930 --> 00:01:11,540
So you should test your bill calculator
25
00:01:11,540 --> 00:01:13,480
for these three bill values.
26
00:01:13,480 --> 00:01:17,630
Okay, now what's important here is that it's not allowed
27
00:01:17,630 --> 00:01:21,150
to use an if else statement, all right.
28
00:01:21,150 --> 00:01:23,290
Now, if it's easier for you,
29
00:01:23,290 --> 00:01:27,150
you can first do this calculation using an if else statement
30
00:01:27,150 --> 00:01:30,340
and then convert that to a ternary operator,
31
00:01:30,340 --> 00:01:33,350
but the final solution should not contain
32
00:01:33,350 --> 00:01:36,230
the if else statement, okay.
33
00:01:36,230 --> 00:01:38,040
Then once you have to calculate it,
34
00:01:38,040 --> 00:01:39,850
print a string to the console,
35
00:01:39,850 --> 00:01:43,940
which will contain the initial bill value, the tip value,
36
00:01:43,940 --> 00:01:46,190
and also the final value.
37
00:01:46,190 --> 00:01:49,410
So that's gonna be the bill plus the tip
38
00:01:49,410 --> 00:01:53,680
and as an example, for example, when the bill is 275,
39
00:01:53,680 --> 00:01:58,290
the output string will be like this, okay.
40
00:01:58,290 --> 00:02:00,820
So once more, I hope you managed to do this
41
00:02:00,820 --> 00:02:04,020
and actually I have two hints to help you solving this,
42
00:02:04,020 --> 00:02:07,220
but if you want to try this on your own without the hints,
43
00:02:07,220 --> 00:02:09,670
then just pause the video right now.
44
00:02:09,670 --> 00:02:13,363
But if you actually need to hints, then well, here they go.
45
00:02:14,280 --> 00:02:18,090
So to calculate, for example, 20% of a value,
46
00:02:18,090 --> 00:02:21,610
all you need to do is to multiply the value by 20
47
00:02:21,610 --> 00:02:25,223
divided by 100, which is 0.2.
48
00:02:26,720 --> 00:02:29,290
So multiply it, okay.
49
00:02:29,290 --> 00:02:32,390
So that's how we calculate the percentage of a value,
50
00:02:32,390 --> 00:02:34,970
which you need here for these rules.
51
00:02:34,970 --> 00:02:38,460
So 15% and 20%
52
00:02:38,460 --> 00:02:40,410
Now as a second hint,
53
00:02:40,410 --> 00:02:44,370
to check whether the value is between 50 and 300,
54
00:02:44,370 --> 00:02:49,370
you do basically this comparison.
55
00:02:49,780 --> 00:02:53,350
So it needs to be greater or equal than 50,
56
00:02:53,350 --> 00:02:56,290
and it needs to be less or equal 300.
57
00:02:56,290 --> 00:03:01,290
That's what it means to be between 50 and 300, okay.
58
00:03:01,600 --> 00:03:02,940
And with these two hints
59
00:03:02,940 --> 00:03:04,920
and the knowledge from the last lecture,
60
00:03:04,920 --> 00:03:07,430
I really hope that you manage to implement
61
00:03:07,430 --> 00:03:09,560
this simple tip calculator.
62
00:03:09,560 --> 00:03:12,343
So pause the video now and I see you in a second.
63
00:03:15,480 --> 00:03:20,480
Okay, so I hope you successfully did that
64
00:03:22,360 --> 00:03:25,043
and so let's try to solve it now together,
65
00:03:26,430 --> 00:03:29,290
or actually let me show you my solution
66
00:03:29,290 --> 00:03:32,400
so that you can see how it compares with yours.
67
00:03:32,400 --> 00:03:34,870
And as always, you can also find my solution
68
00:03:34,870 --> 00:03:38,713
in the final folder, in the GitHub repository, okay.
69
00:03:40,410 --> 00:03:45,090
Now actually let's start with the bill variable,
70
00:03:45,090 --> 00:03:47,940
which I'm gonna set to 275,
71
00:03:47,940 --> 00:03:50,313
which is here the first test value,
72
00:03:51,780 --> 00:03:54,900
and now the tip and let's calculate it
73
00:03:54,900 --> 00:03:56,603
using the ternary operator.
74
00:03:58,670 --> 00:04:00,750
So, let's implement this rule,
75
00:04:00,750 --> 00:04:04,452
which is if the value is between 50 and 300,
76
00:04:04,452 --> 00:04:08,363
then the tip should be 15% of the bill value.
77
00:04:11,730 --> 00:04:16,730
So that means that bill must be less or equal 300,
78
00:04:19,660 --> 00:04:24,660
and at the same time, bill must be greater or equal than 50.
79
00:04:28,190 --> 00:04:32,040
Right, and again, that's what it means for the bill value
80
00:04:32,040 --> 00:04:35,490
to be between 300 and 50.
81
00:04:35,490 --> 00:04:37,760
It's gonna be less or equal 300,
82
00:04:37,760 --> 00:04:42,760
and at the same time, greater or equal than 50.
83
00:04:42,830 --> 00:04:46,330
So 40 for example is of course less than 300,
84
00:04:46,330 --> 00:04:48,840
but it's also less than 50
85
00:04:48,840 --> 00:04:51,440
and so this part here will then be false.
86
00:04:51,440 --> 00:04:54,090
and so this entire expression here,
87
00:04:54,090 --> 00:04:57,963
which is stand true and false will be false as well.
88
00:04:58,960 --> 00:05:01,580
Right, so in this case,
89
00:05:01,580 --> 00:05:06,333
the tip here will be the bill value times 15%,
90
00:05:08,690 --> 00:05:10,713
which as I explained here,
91
00:05:11,730 --> 00:05:15,630
all we need to do is to multiply it by 15 divided by 100.
92
00:05:15,630 --> 00:05:20,560
So that 0.15 and else it's 20%.
93
00:05:22,410 --> 00:05:27,410
So bill times 0.2, give it a safe and that's actually it.
94
00:05:29,790 --> 00:05:31,003
So that's number one.
95
00:05:33,100 --> 00:05:35,210
And now as number two,
96
00:05:35,210 --> 00:05:38,550
let's lock the string to the console
97
00:05:38,550 --> 00:05:41,390
as is requested here in number two.
98
00:05:41,390 --> 00:05:44,530
So let's actually grab this template here
99
00:05:44,530 --> 00:05:47,420
so then we just have to replace the values,
100
00:05:47,420 --> 00:05:49,960
and so since we're building a string, we are again,
101
00:05:49,960 --> 00:05:52,113
relying on the template literal.
102
00:05:53,660 --> 00:05:55,700
So here we're gonna put the bill value
103
00:05:58,810 --> 00:06:00,970
here we're gonna put the tip
104
00:06:02,300 --> 00:06:04,430
and we're missing the curly braces
105
00:06:04,430 --> 00:06:09,430
and here the total value is gonna be the bill plus the tip.
106
00:06:10,410 --> 00:06:12,510
So we can, as you already know,
107
00:06:12,510 --> 00:06:17,230
write an expression here using this operator
108
00:06:17,230 --> 00:06:18,503
so the plus operator,
109
00:06:20,470 --> 00:06:22,270
and that's actually it.
110
00:06:22,270 --> 00:06:25,310
So let's try it and with this bill value,
111
00:06:25,310 --> 00:06:27,970
the output should actually be exactly
112
00:06:27,970 --> 00:06:32,110
like it's written up there and it is.
113
00:06:32,110 --> 00:06:37,110
So 275, the tip is a 4125, so just like here
114
00:06:37,480 --> 00:06:41,053
and the total value is 316.25.
115
00:06:41,890 --> 00:06:45,000
Great, and just to understand this one,
116
00:06:45,000 --> 00:06:46,890
so this condition a little bit better,
117
00:06:46,890 --> 00:06:50,823
let's just take this one and paste it here into the console.
118
00:06:51,870 --> 00:06:54,340
And so we see that this one is true
119
00:06:54,340 --> 00:06:58,644
and of course that's because 275 is less than 300
120
00:06:58,644 --> 00:07:00,330
and more than 50.
121
00:07:00,330 --> 00:07:03,210
So it is between this range and therefore
122
00:07:03,210 --> 00:07:07,070
this part of the ternary operator here will be executed
123
00:07:07,070 --> 00:07:10,430
and to become the result of this operator, essentially.
124
00:07:10,430 --> 00:07:13,100
Right, so the result of this operator
125
00:07:13,100 --> 00:07:16,870
will be bill times 0.15 and that's the value
126
00:07:16,870 --> 00:07:21,560
that will then be assigned to the tip variable, okay.
127
00:07:21,560 --> 00:07:23,090
Now let's try another one.
128
00:07:23,090 --> 00:07:28,090
So 40, and now the tip was eight.
129
00:07:30,500 --> 00:07:32,043
Let's try this one here again,
130
00:07:33,350 --> 00:07:35,430
and now the condition is false.
131
00:07:35,430 --> 00:07:37,460
So remember that here in the console,
132
00:07:37,460 --> 00:07:39,330
we have access to all the variables
133
00:07:39,330 --> 00:07:41,170
that we have declared here.
134
00:07:41,170 --> 00:07:45,210
So if I write bill now, you will see that it's actually 40.
135
00:07:45,210 --> 00:07:50,163
So bill is obviously below 300, but it's also below 50.
136
00:07:51,330 --> 00:07:55,600
So this part is false and true and false gives false.
137
00:07:55,600 --> 00:07:57,500
And so in this case, the value
138
00:07:57,500 --> 00:08:02,320
that's gonna be assigned to tip is 40 times 0.2
139
00:08:02,320 --> 00:08:04,320
and that's exactly eight.
140
00:08:04,320 --> 00:08:07,000
So it's what we see here.
141
00:08:07,000 --> 00:08:10,100
Okay, and now just for the sake of completion,
142
00:08:10,100 --> 00:08:11,640
let's do the last one as well
143
00:08:14,370 --> 00:08:18,360
and now this should have worked as well
144
00:08:18,360 --> 00:08:21,787
because now this one is false again
145
00:08:21,787 --> 00:08:26,360
and that's because this time 430 is above 300
146
00:08:26,360 --> 00:08:31,360
and so 86 should be 430 times 0.2.
147
00:08:32,570 --> 00:08:37,280
Let's quickly check that 0.2 and that's right.
148
00:08:37,280 --> 00:08:39,603
That's the 86 that we have up here.
149
00:08:40,620 --> 00:08:44,930
Great, so hopefully you did this and once more,
150
00:08:44,930 --> 00:08:46,597
if you did it a little bit differently,
151
00:08:46,597 --> 00:08:48,530
that's not a problem at all.
152
00:08:48,530 --> 00:08:51,890
I just hope that you made it work, okay.
153
00:08:51,890 --> 00:08:55,750
And actually this concludes the coding part of this section
154
00:08:55,750 --> 00:08:57,810
there's just one more lecture left,
155
00:08:57,810 --> 00:09:01,580
which is going to be a deeper dive into JavaScript versions,
156
00:09:01,580 --> 00:09:04,690
especially into and ES5 and ES6.
157
00:09:04,690 --> 00:09:08,180
So it's gonna be an interesting one and yeah,
158
00:09:08,180 --> 00:09:09,480
I'm waiting for you there.
12431
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.