All language subtitles for 15. Basic Arithmetic and the Modulo Operator in Javascript

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,420 --> 00:00:04,980 Now for the most part working in numbers and Javascript is pretty straightforward. 1 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 2 3 00:00:12,270 --> 00:00:14,910 or the minus sign. When you want to multiply, 3 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. 4 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 5 6 00:00:29,490 --> 00:00:30,060 sign. 6 7 00:00:30,120 --> 00:00:33,840 It looks almost like we're trying to divide nine by six, 7 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 8 9 00:00:41,250 --> 00:00:43,820 the remainder of the division. 9 10 00:00:43,920 --> 00:00:46,140 So 9 divide by 6. 10 11 00:00:46,140 --> 00:00:47,810 So 6 goes into 9 11 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. 12 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 13 14 00:01:05,580 --> 00:01:08,110 of what 6 divided by 4 will be. 14 15 00:01:09,240 --> 00:01:13,530 Now what do you think 12 modulo 8 will give you? 15 16 00:01:13,530 --> 00:01:13,940 Right. 16 17 00:01:14,010 --> 00:01:17,860 So 8 goes into 12 only once and the remainder is 4. 17 18 00:01:18,090 --> 00:01:19,590 Now why is this useful? 18 19 00:01:19,590 --> 00:01:21,780 Why do we even have this operator? 19 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 20 21 00:01:30,780 --> 00:01:32,540 it by 2, 21 22 00:01:32,850 --> 00:01:38,570 and if the modulo is 0 then it will be a even number, 22 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 23 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. 24 25 00:01:50,070 --> 00:01:51,740 Now that was pretty easy, right? 25 26 00:01:51,930 --> 00:01:53,660 But what about this expression? 26 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. 27 28 00:01:59,330 --> 00:02:02,980 What does this equal? If we first added 3 to 5, 28 29 00:02:03,060 --> 00:02:04,810 then we multiply that by 2, 29 30 00:02:04,860 --> 00:02:06,200 we would get 16. 30 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. 31 32 00:02:12,300 --> 00:02:17,000 Now in math the ambiguity is resolved by something called precedence. 32 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. 33 34 00:02:23,640 --> 00:02:26,660 And this works exactly the same way in programming. 34 35 00:02:26,820 --> 00:02:34,140 So this expression will evaluate to 13 because it will carry out the multiplication first. 35 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 36 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. 37 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 38 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. 39 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 40 41 00:03:07,020 --> 00:03:08,060 this expression. 41 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. 42 43 00:03:13,950 --> 00:03:18,240 I want you to create a dog age to human age converter. 43 44 00:03:18,240 --> 00:03:21,960 So how old would your dog be if it was a human? 44 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 45 46 00:03:27,840 --> 00:03:28,880 perspective. 46 47 00:03:29,100 --> 00:03:34,520 So you're going to subtract 2 from the dog age first, then multiply 4, 47 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 48 49 00:03:40,500 --> 00:03:47,130 Developer Tools, going into Sources and creating your code inside your index.js snippet. 49 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. 50 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. 51 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. 52 53 00:04:00,900 --> 00:04:05,830 So pause the video and see if you can complete this challenge. 53 54 00:04:05,970 --> 00:04:08,730 All right. So let's tackle this. 54 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 55 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 56 57 00:04:21,000 --> 00:04:21,760 dog?'. 57 58 00:04:22,230 --> 00:04:26,150 Now once we've got that number then we can carry out the formula. 58 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 59 60 00:04:32,240 --> 00:04:38,870 age, and then we have to multiply this by 4 and then we add 21. 60 61 00:04:38,880 --> 00:04:43,090 Now this, as I've written it here, is a very ambiguous statement. 61 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. 62 63 00:04:47,550 --> 00:04:50,500 And this will likely not give us the right answer. 63 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 64 65 00:04:57,030 --> 00:04:58,270 some parentheses. 65 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 66 67 00:05:04,050 --> 00:05:07,080 going to be multiplied by 4, 67 68 00:05:07,590 --> 00:05:11,820 and finally it's going to have 21 added to it. 68 69 00:05:11,820 --> 00:05:16,290 Now, even though the second set of parentheses were not entirely necessary, 69 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 70 71 00:05:23,790 --> 00:05:25,280 are going to take place. 71 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 72 73 00:05:36,000 --> 00:05:37,960 in human years. 73 74 00:05:38,650 --> 00:05:39,220 All right. Cool. 74 75 00:05:39,220 --> 00:05:41,800 So let's hit run and give this a go. 75 76 00:05:41,800 --> 00:05:42,820 How old is your dog? 76 77 00:05:42,820 --> 00:05:44,490 My dog is 7 years old. 77 78 00:05:44,500 --> 00:05:44,860 Let's hit 78 79 00:05:44,860 --> 00:05:45,600 OK. 79 80 00:05:45,700 --> 00:05:50,920 And it tells us that the dog is 41 years old in human years. 80 81 00:05:51,040 --> 00:05:52,480 So that was pretty cool. 81 82 00:05:52,480 --> 00:05:53,700 Did you get it right? 82 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. 83 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 84 85 00:06:04,990 --> 00:06:09,980 can do with Javascript numbers that you might not have seen in maths. 85 86 00:06:10,090 --> 00:06:13,110 So for all of that and more, I'll see you on the next lesson. 8842

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.