Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,420 --> 00:00:04,980
Now for the most part working in numbers and Javascript is pretty straightforward.
2
00:00:05,190 --> 00:00:12,240
So for example you can add by simply using the plus sign and you can subtract by simply using the dash
3
00:00:12,270 --> 00:00:14,910
or the minus sign. When you want to multiply,
4
00:00:14,910 --> 00:00:21,720
you can use the asterisk as the multiply sign, and division is done with a single forward slash.
5
00:00:21,720 --> 00:00:29,490
Now the only one operator that's a little bit weird is the modulo, and it's represented by a percentage
6
00:00:29,490 --> 00:00:30,060
sign.
7
00:00:30,120 --> 00:00:33,840
It looks almost like we're trying to divide nine by six,
8
00:00:33,900 --> 00:00:41,250
if you just try to imagine the similarity with the division symbol, but it's actually going to give you
9
00:00:41,250 --> 00:00:43,820
the remainder of the division.
10
00:00:43,920 --> 00:00:46,140
So 9 divide by 6.
11
00:00:46,140 --> 00:00:47,810
So 6 goes into 9
12
00:00:47,820 --> 00:00:55,370
only one time, and you get a remainder of 3, which is going to be the outcome of this expression.
13
00:00:55,530 --> 00:01:05,490
So we know that 6 divided by 4 is 1.5, but 6 modulo 4 is going to give us 2, because that's the remainder
14
00:01:05,580 --> 00:01:08,110
of what 6 divided by 4 will be.
15
00:01:09,240 --> 00:01:13,530
Now what do you think 12 modulo 8 will give you?
16
00:01:13,530 --> 00:01:13,940
Right.
17
00:01:14,010 --> 00:01:17,860
So 8 goes into 12 only once and the remainder is 4.
18
00:01:18,090 --> 00:01:19,590
Now why is this useful?
19
00:01:19,590 --> 00:01:21,780
Why do we even have this operator?
20
00:01:21,900 --> 00:01:30,780
Well, if you are trying to work out if a number that you have is odd or even, say 45, you could divide
21
00:01:30,780 --> 00:01:32,540
it by 2,
22
00:01:32,850 --> 00:01:38,570
and if the modulo is 0 then it will be a even number,
23
00:01:38,820 --> 00:01:44,640
but if the modulo is not 0 then it will be an odd number. And there's various other ways that you can
24
00:01:44,640 --> 00:01:50,070
use the modulo in your programming, which we'll come to see in our future lessons.
25
00:01:50,070 --> 00:01:51,740
Now that was pretty easy, right?
26
00:01:51,930 --> 00:01:53,660
But what about this expression?
27
00:01:53,760 --> 00:01:59,280
Say if we create a variable called cost, and it's equal to 3 + 5 * 2.
28
00:01:59,330 --> 00:02:02,980
What does this equal? If we first added 3 to 5,
29
00:02:03,060 --> 00:02:04,810
then we multiply that by 2,
30
00:02:04,860 --> 00:02:06,200
we would get 16.
31
00:02:06,540 --> 00:02:12,280
But if on the other hand we first multiply the 5 by 2 and then add 3, we get 13.
32
00:02:12,300 --> 00:02:17,000
Now in math the ambiguity is resolved by something called precedence.
33
00:02:17,130 --> 00:02:23,550
There's a rule that says we must first multiply and divide before we add or subtract.
34
00:02:23,640 --> 00:02:26,660
And this works exactly the same way in programming.
35
00:02:26,820 --> 00:02:34,140
So this expression will evaluate to 13 because it will carry out the multiplication first.
36
00:02:34,140 --> 00:02:42,150
Now if we wanted to get 16 from this expression then we can use a set of parentheses to make our intentions
37
00:02:42,150 --> 00:02:50,010
clear, to say that we first want to add 5 to 3 before we carry out the multiplication.
38
00:02:50,010 --> 00:02:55,350
And in order for your code to be easily readable and easily understood, it's good programming practice
39
00:02:55,470 --> 00:03:01,860
to add a set of parentheses even when you know that the multiplication is going to happen first.
40
00:03:01,860 --> 00:03:07,020
This makes it abundantly clear what is going to happen first and what is going to be the outcome of
41
00:03:07,020 --> 00:03:08,060
this expression.
42
00:03:08,070 --> 00:03:13,850
All right. So now it's time to use what you've learned and complete a challenge.
43
00:03:13,950 --> 00:03:18,240
I want you to create a dog age to human age converter.
44
00:03:18,240 --> 00:03:21,960
So how old would your dog be if it was a human?
45
00:03:21,990 --> 00:03:27,840
And this is the formula that you're going to use. And this formula is completely valid from a mathematical
46
00:03:27,840 --> 00:03:28,880
perspective.
47
00:03:29,100 --> 00:03:34,520
So you're going to subtract 2 from the dog age first, then multiply 4,
48
00:03:34,620 --> 00:03:40,500
then add 21. And I want you to use what you've learned to create this converter by going into the Chrome
49
00:03:40,500 --> 00:03:47,130
Developer Tools, going into Sources and creating your code inside your index.js snippet.
50
00:03:47,220 --> 00:03:51,820
So your code is going to create a prompt to ask the user for the age of that dog.
51
00:03:51,960 --> 00:03:56,120
Then it's going to calculate the equivalent human age of the dog using this formula.
52
00:03:56,370 --> 00:04:00,900
And then it's going to give this answer back to the user through an alert.
53
00:04:00,900 --> 00:04:05,830
So pause the video and see if you can complete this challenge.
54
00:04:05,970 --> 00:04:08,730
All right. So let's tackle this.
55
00:04:08,730 --> 00:04:13,500
Now the first thing we're going to do is we're going to create a variable called dogAge, and this is
56
00:04:13,500 --> 00:04:21,000
going to be set to equal the answer that we get from the prompt where we ask the user 'How old is your
57
00:04:21,000 --> 00:04:21,760
dog?'.
58
00:04:22,230 --> 00:04:26,150
Now once we've got that number then we can carry out the formula.
59
00:04:26,190 --> 00:04:32,240
So our formula states that in order to calculate the human age we have to subtract 2 from the dog
60
00:04:32,240 --> 00:04:38,870
age, and then we have to multiply this by 4 and then we add 21.
61
00:04:38,880 --> 00:04:43,090
Now this, as I've written it here, is a very ambiguous statement.
62
00:04:43,140 --> 00:04:47,510
I have no idea which part is going to be carried out first and which part last.
63
00:04:47,550 --> 00:04:50,500
And this will likely not give us the right answer.
64
00:04:50,670 --> 00:04:57,030
So let's go ahead and add some precedence rules and also make our expression more clear by adding in
65
00:04:57,030 --> 00:04:58,270
some parentheses.
66
00:04:58,290 --> 00:05:04,050
So the first thing that we need to happen is that dog age needs to be subtracted by 2, and then it's
67
00:05:04,050 --> 00:05:07,080
going to be multiplied by 4,
68
00:05:07,590 --> 00:05:11,820
and finally it's going to have 21 added to it.
69
00:05:11,820 --> 00:05:16,290
Now, even though the second set of parentheses were not entirely necessary,
70
00:05:16,470 --> 00:05:23,430
it does however make our code look abundantly clear as to exactly what the order of the calculations
71
00:05:23,790 --> 00:05:25,280
are going to take place.
72
00:05:25,290 --> 00:05:35,880
So finally we can send an alert back to our human user and we can say, your dog is this many years old
73
00:05:36,000 --> 00:05:37,960
in human years.
74
00:05:38,650 --> 00:05:39,220
All right. Cool.
75
00:05:39,220 --> 00:05:41,800
So let's hit run and give this a go.
76
00:05:41,800 --> 00:05:42,820
How old is your dog?
77
00:05:42,820 --> 00:05:44,490
My dog is 7 years old.
78
00:05:44,500 --> 00:05:44,860
Let's hit
79
00:05:44,860 --> 00:05:45,600
OK.
80
00:05:45,700 --> 00:05:50,920
And it tells us that the dog is 41 years old in human years.
81
00:05:51,040 --> 00:05:52,480
So that was pretty cool.
82
00:05:52,480 --> 00:05:53,700
Did you get it right?
83
00:05:53,710 --> 00:05:59,430
If not, take a look back at this lesson and try to refresh yourself on some of those concepts.
84
00:05:59,440 --> 00:06:04,990
So in the next lesson we're going to look at some slightly unfamiliar mathematical operations that you
85
00:06:04,990 --> 00:06:09,980
can do with Javascript numbers that you might not have seen in maths.
86
00:06:10,090 --> 00:06:13,110
So for all of that and more, I'll see you on the next lesson.
8166
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.