Basic arithmetic often comes in handy when programming.
The first four work how you might guess:
console.log(3 + 4); // Prints 7 console.log(5 - 1); // Prints 4 console.log(4 * 2); // Prints 8 console.log(9 / 3); // Prints 3
Note that when we
console.log() the computer will evaluate the expression inside the parentheses and print that result to the console. If we wanted to print the characters
3 + 4, we would wrap them in quotes and print them as a string.
console.log(11 % 3); // Prints 2 console.log(12 % 3); // Prints 0
The remainder operator, sometimes called modulo, returns the number that remains after the right-hand number divides into the left-hand number as many times as it evenly can:
11 % 3 equals 2 because 3 fits into 11 three times, leaving 2 as the remainder.
Inside of a
3.5 to your age.
This is the age you’ll be when we start sending people to live on Mars.
On a new line write another
console.log(). Inside the parentheses, take the current year and subtract
The answer is how many years it’s been since the 1969 moon landing.
console.log(). Inside the parentheses divide
Create one last
console.log(). Inside the parentheses, multiply
That’s the percent of the sun that is made up of helium. Assuming we could stand on the sun, we’d all sound like chipmunks!