Math, Mod, and More!
Goals:
- Learn about and use the mod and integer division operators, and explore different computational resources from the
Math
object
Coding Snippets:
- Use the integer division operator,
//
, to compute the quotient resulting from the division of one integer by another. - Use the modulus (or mod) operator,
%
, to compute a remainder from division. - Cause expressions inside strings to be evaluated using string interpolation by enclosing them inside of
#{}
.
Math and Computer Concepts:
- Four terms are used to describe the division of one value by another:
- In addition to the division operator (
/
), Javascript provides the operators//
and%
to provide direct access to thequotient
and the remainder, respectively. Math
is a built-in Javascript object with properties and functions for mathematical constants and functions, including:Math.ceil(5.2)
6 Math.floor(4.6)
4 Math.round(4.6)
5 Math.max(6, 2, -3.5, 1)
6 Math.sign(-3)
-1 Math.abs(-3)
3 Math.sqrt(9)
3 Math.PI
3.14159....
Activities:
Save each of the following in a folder called Math.
- In a script titled ElementaryDivision, generate two random numbers between 1 and 1000. Compute the quotient and remainder, reporting your findings using labels. Do this two ways. First, use only a single label, creating it using template literals rather than with concatenation. Second, format your output more aesthetically using multiple labels positioned appropriately:
- BrokenMath: What is "0.1 + 0.1 + 0.1"? You might be surprised by the answer Javascript gives. Write a program that uses a for loop to create and print a table of the sums of tenths, i.e.,
0.1
,0.1 + 0.1
,0.1 + 0.1 + 0.1
, up to the sum of 10 tenths. Then devise a means to "fix" these problems using functions introduced in this lesson. - Countdown: Show the exact number of days, hours, minutes, and seconds until your next birthday or some other important upcoming event.
- Contruct a Checkerboard comprised of equally sized squares, where the size is randomly chosen. Fit as large a checkerboard on screen as possible.