Lesson 31 of 48
65%
Math Objects
Math is a built in toolbox for numbers.
You do not make a new Math. You just use Math.
Think of a shared calculator on the teacher desk.
1
Rounding
2
Min and max
3
Random
4
Power and square root
5
Useful constants
Rounding helpers
round, floor, and ceil clean up decimals.
javascript
min and max
These pick the smallest or biggest value.
javascript
random
Math.random() gives a decimal from 0 up to but not including 1.
Combine it with floor to pick a whole number.
javascript
pow and sqrt
pow raises to a power. sqrt finds the square root.
javascript
abs
abs removes the minus sign.
Distance is often easier as a positive number.
javascript
Constants
Math.PI is a famous circle number.
javascript
Tip: For a random whole number from 1 to n, use Math.floor(Math.random() * n) + 1.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.