WEBVTT 0 00:00.420 --> 00:04.980 Now for the most part working in numbers and Javascript is pretty straightforward. 1 00:05.190 --> 00:12.240 So for example you can add by simply using the plus sign and you can subtract by simply using the dash 2 00:12.270 --> 00:14.910 or the minus sign. When you want to multiply, 3 00:14.910 --> 00:21.720 you can use the asterisk as the multiply sign, and division is done with a single forward slash. 4 00:21.720 --> 00:29.490 Now the only one operator that's a little bit weird is the modulo, and it's represented by a percentage 5 00:29.490 --> 00:30.060 sign. 6 00:30.120 --> 00:33.840 It looks almost like we're trying to divide nine by six, 7 00:33.900 --> 00:41.250 if you just try to imagine the similarity with the division symbol, but it's actually going to give you 8 00:41.250 --> 00:43.820 the remainder of the division. 9 00:43.920 --> 00:46.140 So 9 divide by 6. 10 00:46.140 --> 00:47.810 So 6 goes into 9 11 00:47.820 --> 00:55.370 only one time, and you get a remainder of 3, which is going to be the outcome of this expression. 12 00:55.530 --> 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 13 01:05.580 --> 01:08.110 of what 6 divided by 4 will be. 14 01:09.240 --> 01:13.530 Now what do you think 12 modulo 8 will give you? 15 01:13.530 --> 01:13.940 Right. 16 01:14.010 --> 01:17.860 So 8 goes into 12 only once and the remainder is 4. 17 01:18.090 --> 01:19.590 Now why is this useful? 18 01:19.590 --> 01:21.780 Why do we even have this operator? 19 01:21.900 --> 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 20 01:30.780 --> 01:32.540 it by 2, 21 01:32.850 --> 01:38.570 and if the modulo is 0 then it will be a even number, 22 01:38.820 --> 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 23 01:44.640 --> 01:50.070 use the modulo in your programming, which we'll come to see in our future lessons. 24 01:50.070 --> 01:51.740 Now that was pretty easy, right? 25 01:51.930 --> 01:53.660 But what about this expression? 26 01:53.760 --> 01:59.280 Say if we create a variable called cost, and it's equal to 3 + 5 * 2. 27 01:59.330 --> 02:02.980 What does this equal? If we first added 3 to 5, 28 02:03.060 --> 02:04.810 then we multiply that by 2, 29 02:04.860 --> 02:06.200 we would get 16. 30 02:06.540 --> 02:12.280 But if on the other hand we first multiply the 5 by 2 and then add 3, we get 13. 31 02:12.300 --> 02:17.000 Now in math the ambiguity is resolved by something called precedence. 32 02:17.130 --> 02:23.550 There's a rule that says we must first multiply and divide before we add or subtract. 33 02:23.640 --> 02:26.660 And this works exactly the same way in programming. 34 02:26.820 --> 02:34.140 So this expression will evaluate to 13 because it will carry out the multiplication first. 35 02:34.140 --> 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 36 02:42.150 --> 02:50.010 clear, to say that we first want to add 5 to 3 before we carry out the multiplication. 37 02:50.010 --> 02:55.350 And in order for your code to be easily readable and easily understood, it's good programming practice 38 02:55.470 --> 03:01.860 to add a set of parentheses even when you know that the multiplication is going to happen first. 39 03:01.860 --> 03:07.020 This makes it abundantly clear what is going to happen first and what is going to be the outcome of 40 03:07.020 --> 03:08.060 this expression. 41 03:08.070 --> 03:13.850 All right. So now it's time to use what you've learned and complete a challenge. 42 03:13.950 --> 03:18.240 I want you to create a dog age to human age converter. 43 03:18.240 --> 03:21.960 So how old would your dog be if it was a human? 44 03:21.990 --> 03:27.840 And this is the formula that you're going to use. And this formula is completely valid from a mathematical 45 03:27.840 --> 03:28.880 perspective. 46 03:29.100 --> 03:34.520 So you're going to subtract 2 from the dog age first, then multiply 4, 47 03:34.620 --> 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 48 03:40.500 --> 03:47.130 Developer Tools, going into Sources and creating your code inside your index.js snippet. 49 03:47.220 --> 03:51.820 So your code is going to create a prompt to ask the user for the age of that dog. 50 03:51.960 --> 03:56.120 Then it's going to calculate the equivalent human age of the dog using this formula. 51 03:56.370 --> 04:00.900 And then it's going to give this answer back to the user through an alert. 52 04:00.900 --> 04:05.830 So pause the video and see if you can complete this challenge. 53 04:05.970 --> 04:08.730 All right. So let's tackle this. 54 04:08.730 --> 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 55 04:13.500 --> 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 56 04:21.000 --> 04:21.760 dog?'. 57 04:22.230 --> 04:26.150 Now once we've got that number then we can carry out the formula. 58 04:26.190 --> 04:32.240 So our formula states that in order to calculate the human age we have to subtract 2 from the dog 59 04:32.240 --> 04:38.870 age, and then we have to multiply this by 4 and then we add 21. 60 04:38.880 --> 04:43.090 Now this, as I've written it here, is a very ambiguous statement. 61 04:43.140 --> 04:47.510 I have no idea which part is going to be carried out first and which part last. 62 04:47.550 --> 04:50.500 And this will likely not give us the right answer. 63 04:50.670 --> 04:57.030 So let's go ahead and add some precedence rules and also make our expression more clear by adding in 64 04:57.030 --> 04:58.270 some parentheses. 65 04:58.290 --> 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 66 05:04.050 --> 05:07.080 going to be multiplied by 4, 67 05:07.590 --> 05:11.820 and finally it's going to have 21 added to it. 68 05:11.820 --> 05:16.290 Now, even though the second set of parentheses were not entirely necessary, 69 05:16.470 --> 05:23.430 it does however make our code look abundantly clear as to exactly what the order of the calculations 70 05:23.790 --> 05:25.280 are going to take place. 71 05:25.290 --> 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 72 05:36.000 --> 05:37.960 in human years. 73 05:38.650 --> 05:39.220 All right. Cool. 74 05:39.220 --> 05:41.800 So let's hit run and give this a go. 75 05:41.800 --> 05:42.820 How old is your dog? 76 05:42.820 --> 05:44.490 My dog is 7 years old. 77 05:44.500 --> 05:44.860 Let's hit 78 05:44.860 --> 05:45.600 OK. 79 05:45.700 --> 05:50.920 And it tells us that the dog is 41 years old in human years. 80 05:51.040 --> 05:52.480 So that was pretty cool. 81 05:52.480 --> 05:53.700 Did you get it right? 82 05:53.710 --> 05:59.430 If not, take a look back at this lesson and try to refresh yourself on some of those concepts. 83 05:59.440 --> 06:04.990 So in the next lesson we're going to look at some slightly unfamiliar mathematical operations that you 84 06:04.990 --> 06:09.980 can do with Javascript numbers that you might not have seen in maths. 85 06:10.090 --> 06:13.110 So for all of that and more, I'll see you on the next lesson.