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

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French Download
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

0 Now for the most part working in numbers and Javascript is pretty straightforward. 1

So for example you can add by simply using the plus sign and you can subtract by simply using the dash 2

or the minus sign. When you want to multiply, 3

you can use the asterisk as the multiply sign, and division is done with a single forward slash. 4

Now the only one operator that's a little bit weird is the modulo, and it's represented by a percentage 5

sign. 6

It looks almost like we're trying to divide nine by six, 7

if you just try to imagine the similarity with the division symbol, but it's actually going to give you 8

the remainder of the division. 9

So 9 divide by 6. 10

So 6 goes into 9 11

only one time, and you get a remainder of 3, which is going to be the outcome of this expression. 12

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

of what 6 divided by 4 will be. 14

Now what do you think 12 modulo 8 will give you? 15

Right. 16

So 8 goes into 12 only once and the remainder is 4. 17

Now why is this useful? 18

Why do we even have this operator? 19

Well, if you are trying to work out if a number that you have is odd or even, say 45, you could divide 20

it by 2, 21

and if the modulo is 0 then it will be a even number, 22

but if the modulo is not 0 then it will be an odd number. And there's various other ways that you can 23

use the modulo in your programming, which we'll come to see in our future lessons. 24

Now that was pretty easy, right? 25

But what about this expression? 26

Say if we create a variable called cost, and it's equal to 3 + 5 * 2. 27

What does this equal? If we first added 3 to 5, 28

then we multiply that by 2, 29

we would get 16. 30

But if on the other hand we first multiply the 5 by 2 and then add 3, we get 13. 31

Now in math the ambiguity is resolved by something called precedence. 32

There's a rule that says we must first multiply and divide before we add or subtract. 33

And this works exactly the same way in programming. 34

So this expression will evaluate to 13 because it will carry out the multiplication first. 35

Now if we wanted to get 16 from this expression then we can use a set of parentheses to make our intentions 36

clear, to say that we first want to add 5 to 3 before we carry out the multiplication. 37

And in order for your code to be easily readable and easily understood, it's good programming practice 38

to add a set of parentheses even when you know that the multiplication is going to happen first. 39

This makes it abundantly clear what is going to happen first and what is going to be the outcome of 40

this expression. 41

All right. So now it's time to use what you've learned and complete a challenge. 42

I want you to create a dog age to human age converter. 43

So how old would your dog be if it was a human? 44

And this is the formula that you're going to use. And this formula is completely valid from a mathematical 45

perspective. 46

So you're going to subtract 2 from the dog age first, then multiply 4, 47

then add 21. And I want you to use what you've learned to create this converter by going into the Chrome 48

Developer Tools, going into Sources and creating your code inside your index.js snippet. 49

So your code is going to create a prompt to ask the user for the age of that dog. 50

Then it's going to calculate the equivalent human age of the dog using this formula. 51

And then it's going to give this answer back to the user through an alert. 52

So pause the video and see if you can complete this challenge. 53

All right. So let's tackle this. 54

Now the first thing we're going to do is we're going to create a variable called dogAge, and this is 55

going to be set to equal the answer that we get from the prompt where we ask the user 'How old is your 56

dog?'. 57

Now once we've got that number then we can carry out the formula. 58

So our formula states that in order to calculate the human age we have to subtract 2 from the dog 59

age, and then we have to multiply this by 4 and then we add 21. 60

Now this, as I've written it here, is a very ambiguous statement. 61

I have no idea which part is going to be carried out first and which part last. 62

And this will likely not give us the right answer. 63

So let's go ahead and add some precedence rules and also make our expression more clear by adding in 64

some parentheses. 65

So the first thing that we need to happen is that dog age needs to be subtracted by 2, and then it's 66

going to be multiplied by 4, 67

and finally it's going to have 21 added to it. 68

Now, even though the second set of parentheses were not entirely necessary, 69

it does however make our code look abundantly clear as to exactly what the order of the calculations 70

are going to take place. 71

So finally we can send an alert back to our human user and we can say, your dog is this many years old 72

in human years. 73

All right. Cool. 74

So let's hit run and give this a go. 75

How old is your dog? 76

My dog is 7 years old. 77

Let's hit 78

OK. 79

And it tells us that the dog is 41 years old in human years. 80

So that was pretty cool. 81

Did you get it right? 82

If not, take a look back at this lesson and try to refresh yourself on some of those concepts. 83

So in the next lesson we're going to look at some slightly unfamiliar mathematical operations that you 84

can do with Javascript numbers that you might not have seen in maths. 85

So for all of that and more, I'll see you on the next lesson.

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