Lessons 110 to 111 – Complete 2020 Web Development Bootcamp

Lesson 110 – Basic Arithmetic and the Modulo Operator

To add numbers in Javascript, you simply use the plus sign. For example,

var a = 3 + 2;

To subtract numbers, you use the minus sign. So –

var b = 10 - 5;

To multiply you use the asterisk sign and to divide you use a single forward slash. So –

var c = 4 * 5;


var d = 6 / 2;

The modulo operator is represented by a percentage sign.

var e = 9 % 6;

The modulo operator gives you the remainder of the division. In the above example, 6 goes into 9 only one time and you get a remainder of 3 which is the outcome of 9 % 6.

The modulo operator is useful for working out whether a particular number you are working with is odd or even. To determine if the number is odd or even you can use “% 2”. If the modulo is 0 then it is an even number but if the modulo is not 0 then it is an odd number. For example,

45 % 2; 

will produce a modulo of 1 so 45 is an odd number.

46 % 2;

will produce a modulo of 0 so it is an even number.

The math rule of precedence will determine in what order calculations are made. Multiplication and division are done first and then addition and subtraction. So the following example will evaluate to the number 13.

var cost = 3 + 5 * 2;

Javascript will first multiply 5 x 2 and then add 3.

To change the usual order of precedence you can use parenthesis in your code. The next example evaluates to the number 16 because of the use of parenthesis.

var cost = (3 + 5) * 2;

Javascript first adds 3 and 5 and then multiplies by 2.

The challenge in this lesson is to:

  1. Create a prompt that asks the user for the age of his dog;
  2. Use the supplied formula to convert the age of the dog in dog years to the age of the dog in human years;
  3. To alert the user with a message telling the user the age of his dog in human years.

This is my solution –

var dogAge = prompt("What is your dog's age?");
var humanAge = (dogAge - 2) * 4 + 21;
alert("Your dog is " + humanAge + " years old in human years!");

This is slightly different than Angela’s solution. Her solution was:

var dogAge = prompt("What is your dog's age?");
var humanAge = ((dogAge - 2) * 4) + 21;
alert("Your dog is " + humanAge + " years old in human years!");

The difference is that on “var humanAge” line she added an additional pair of parenthesis that surround “(dogAge – 2) * 4”. This pair of parenthesis are not strictly necessary as the code runs in the same way without them but it is a good idea to add them as they make it clear to anyone reading the code that the first calculation Javascript will make is to subtract 2 from the variable “dogAge”. That total will then be multiplied by 4 and finally that total will be added to 21.

Lesson 111 – Increment and Decrement Expressions

You can increment a number in Javascript in the following way:

var x = 5;

This code results in x now being equal to 6 as “x++” increments x (which has been defined as 5) with 1 resulting in 6. It is the equivalent of:

var x = 5;
x = x + 1;

You can decrement a number in the same way:

var x = 5;

Once this code is run, x is equivalent to 4 as x (originally defined as 5) is decremented by 1 and becomes 4.

This is the same as:

var x = 5;
x = x - 1;

To increment by more than one you can use:

var x = 5;
x += 2;

This will increment 5 by adding 2 and produces 7.

You can decrement by more than 1 in the same way. So –

var x = 5;
x -= 2;

This results in x being equal to 3.

To multiply by more than 1 you use “*=” and to divide by more than one you use “/=”.

It can be used in the same way with variables. For example,

var x = 5;
var y = 3;
x += y;

This adds 5 and 3 and the variable x becomes equal to 8.