MATH OPERATIONS
JavaScript uses the following arithmetic operators:
Symbol Operation Example
+ Addition result = a + b; (result will now store the sum of the values of a and b)
- Subtraction result = a - b; (result will now store the difference of the values of a and b)
* Multiplication result = a * b; (result will now store the product of the values of a and b)
/ Division result = a / b; (result will now store the quotient of the values of a and b)
% Modulus result = a % b; (result will now store the remainder of a divided by b)
** Exponentiation result = a ** b; (result will now store a to the power of b)
++ Increment b++; (the value of b will increase by 1); equivalent to b=b+1
-- Decrement b--; (the value of b will decrease by 1); equivalent to b=b-1
Math operations can be used with variables or numbers.
Example: b = a + 5; c = 14 / 7; power = 3 ** exponent;
When using math operations, you can change a variable’s value by using it’s previous value.
Example: var a = 7; //variable a is assigned a value of 7.
a = a * 2; //the RS of the statement is evaluated first. Therefore, the right side
//evaluates to 14 (7 * 2), then that value is assigned to a.
//a now equals 14!
ORDER OF OPERATIONS
JavaScript follows has the same operator precedence you have learned in math class (BEDMAS). You can use
brackets to set a higher precedence for a certain operation.
Examples: var x = 100 + 50 * 3; //x is assigned 250 because * has higher precedence then +
var x = (100 + 50) * 3; //x is assigned 450 because of the brackets
MODULUS OPERATOR
The modulus operator (%) allows you to find the remainder of the division of two integers.
Examples: var x = 8 % 7; //returns 1 because 8 / 7 has a remainder of 1
var x = 18 % 6; //returns 0 because there is no remainder when dividing 18 / 6
var x = 3 % 5; //returns 3 because you cannot divide 5 out of 3
When would you need to use this?
Consider – a course is 110 hours – perhaps you want to know how may days this is?
var days = 110/24; //this is 4.583333333; not really how we would say it. Instead we can use division and
remainder and represent it in days and hours.
days = [Link](110/24); //get the integer value of the 110/24
var hours = 110 % 24; //the remaining hours
[Link]("110 hours is " + days + " days and " + hours + " hours");
MATH OBJECT
The JavaScript Math Object is a built-in library for mathematical constants and functions. Here are some
examples of constants and functions that exist in the object.
Constant or Function Purpose Example
[Link] Gives you the value of PI. Returns the value 3.141592653589793
[Link]() Rounds a number to the nearest whole [Link](5.6) returns 6.
number.
[Link]() Calculates a power. [Link](3,2) returns 9
[Link]() Calculates a square root. [Link](16) returns 4
[Link]() [Link]() rounds the number up. [Link](5.8) returns 5.
[Link]() [Link]() rounds the number down.
Calculates a random number between 0 and
[Link]() less than 1. Can be used to make any range [Link]() generates a random
of random numbers.
There are many other constants and functions available, check them out at [Link] OR MSDN.
GENERATING RANDOM NUMBERS
You can generate any range of random numbers using [Link](). For example, the line of code below
would generate a random whole number between 1 and 20.
var x = [Link](20 * [Link]() + 1);
Here’s how it works.
1. We multiply the result of [Link]() by 20. Our possible values are now between 0 and
19.9999999.
2. We add 1 to that result. Our possible values are now between 1 and 20.99999999999.
3. We use [Link]() to round the result down. Now we are left with a random number between 1
and 20.
A more generalized formula would look like this:
var x = [Link](range * [Link]() + minimum);
Where range describes how many different numbers you need and minimum describes the lowest number in
the range.
What possible values would the following line of code produce?
var x = [Link](40 * [Link]() -10);
// x could be an integer value in the range of -10 to 29 inclusive
// there are a few ways to ensure you included 30 in the values – can you figure them out?
CHECK FOR KNOWLEDGE (ANSWER ON PAPER AND CONFIRM IN PROGRAM)
1. Indicate the possible values of x:
var x = [Link](15 * [Link]() + 12)
2. Write the code necessary to pick a random integer between 1 and 50 (inclusive).
3. Write the code necessary to pick a random number between -10 and 10 (inclusive).
4. Write the code necessary to pick a random number between -67 and 14 (inclusive).
5. What is the outcome of: a) 18 % 4 b) 18 / 4 c) 11 ** 2
PRACTICE EXERCISES (WRITE IN PROGRAM)
1. Write a program that will ask the user for an amount of money less than one dollar. This
amount will be in cents. The program will then calculate the quantity of coins required to
make change.
(HINT: if user's number is 68, the output will be “68 cents is 2 quarters, 1 dimes, 1
nickels, 3 pennies” – you may need %)
2. Write a program that will generate 5 math questions for a user to answer. The numbers
used in the questions should be randomly generated integers. Prompt the user to answer
each question. Write the question and their answer and to the page. When they finish the
questions, print out their result on the screen. Be creative with the complexity of the
questions and how this looks and works! (Note – we are not checking to see IF the user
is correct … yet)
Question: (3+43) * 2
Your Response: 92
and the answer is … 92.
Question: (12 -3) % 5
Your Response: 9
and the answer is … 4