Introduction to Math Methods
#
Learning Objectives- Be able to explain: what is a method?
- Use a Math method to help solve a problem
#
Set Up- In today's
student_examples
folder,touch math.js
#
IntroductionLet's say we are designing a web site and we are incorporating a way to donate to charity. Whatever the purchase price, you can give consumers the option to round up their bill to the nearest dollar and take those extra cents and put them towards a worthy cause.
const cartTotal = 16.07;
How can we round up?
We could write some logic. However, sometimes there are problems that are so common, that a language like JavaScript has a built in way to solve it.
tip
JavaScript has a little 'library' of math functions that we can access by typing Math
and then a .
(dot) and then whatever method we want, finally we put the things we want to evaluate inside the parenthesis.
Let's take a quick look at the docs and see if we can find a method that suits us.
Math.round()
looks like a great contender - let's try it
const cartTotal = 16.07;const roundedUpCart = Math.round(cartTotal);console.log(roundedUpCart);
Hmmm... that gave us 16
. Let's keep looking
There is a Math.ceil
method that will always round up
Let's try it
const cartTotal = 16.07;const roundedUpCart = Math.ceil(cartTotal);console.log(roundedUpCart);
Great! We now have a total of $17
to charge our customers.
Now, we want to subtract the difference from the original total from the new total and store that
const cartTotal = 16.07;const roundedUpCart = Math.ceil(cartTotal);const donation = roundedUpCart - cartTotal;console.log(donation);
Uh oh. We get an unexpected value. Turns out JS is not great with floating point numbers! We'll have to be mindful of this. We can fix this now by using another method .toFixed
const cartTotal = 16.07;const roundedUpCart = Math.ceil(cartTotal);const donation = (roundedUpCart - cartTotal).toFixed(2);console.log(donation);
There are only a handful of methods we'll find handy for most of our work in this course:
Most Likely:
Math.ceil()
- always rounds a floating point number upMath.floor()
- always rounds a floating point number downMath.max()
- finds the largest numberMath.min()
- finds the smallest numberMath.random()
- generates a random number between0
to1
Math.round()
- rounds a floating point number up or down, depending if the decimal is greater than or less than0.5
Less Likely:
Math.abs()
- gives us the absolute valueMath.PI
- gives us the value of PIMath.sqrt()
- finds the square root of a numberMath.pow()
- raises a number to the power