Arithmetic
This online wine order form uses Math
methods to calculate a total and convert the result to dollars and cents. Here's the source code. Observe:
- the Math.floor() method
-- truncates the fractional part of a number (e.g. converts 3.6 to 3)
- the Math.round() method
-- rounds to the nearest integer (e.g. converts 3.6 to 4)
Other methods of Math include:
- Math.exp() -- Returns enumber,
where
number is the argument, and e is Euler's constant,
the base of the natural logarithms.
- Math.log() -- Returns the natural logarithm (base e)
of a number.
- Math.max() -- Returns the greater of two numbers.
- Math.min() -- Returns the lesser of two numbers.
- Math.pow() -- Returns
base to the exponent
power, that is, baseexponent.
- Math.random() -- Returns a pseudo-random number between 0 and 1.
- Math.sin() -- Returns the sine of a number.
- Math.sqrt() -- Returns the square root of a number.
- Math.tan() -- Returns the tangent of a number.
Lab
Your task:
- Copy the example program and add to it the following feature.
- Calculate the tax and add another text field to display it.
- Don't forget to add it to the total amount!
- Hint: Use our moneyFormat() function.
Solution
Don't peek unless you're stuck!
Extra for Experts
- Instead of waiting for the user to click Place Order, recalculate the total
whenever a field's value is changed.
- Solution
- Source code