# Math object

  • The Math object is a built-in object with mathematical constants (properties) and methods
    • Unlike many other built-in objects, Math is a static object which has no constructor, so every property/method starts with Math.

# Frequently used (static) properties

property description
Math.PI (opens new window) a constant that approximately equals 3.14159

# Frequently used (static) methods

methode description
Math.ceil(<x>) (opens new window) round <x> up to the nearest integer
Math.floor(<x>) (opens new window) round <x> down to the nearest integer
Math.round(<x>) (opens new window) round <x> to the nearest integer (>= .5 round up, < .5 round down)
Math.random() (opens new window) generate a random number between 0 (included) and 1 (excluded)
Math.abs(<x>) (opens new window) returns the absolute value of <x>

# Examples

# Cylinder volume

  • Open es6/math/cylinder.html and es6/math/cylinder.js
  • Calculate the volume of a cylinder (and round it to 2 decimal points)
<div class="border-gray">
    <p>
        <label for="radius">radius</label>
        <input type="number" name="radius" id="radius" step=".5" value="5" /> cm *
        <label for="height">height</label>
        <input type="number" name="height" id="height" step=".5" value="2" /> cm
    </p>

    <p id="volume1">Volume = <b></b></p>
    <p id="volume2">Volume (rounded) = <b></b></p>
    <p id="volume3">Volume (rounded to 2 decimal points)= <b></b></p>
</div>
Copied!
1
2
3
4
5
6
7
8
9
10
11
12

# Roll the dice

  • Open es6/math/dice.html and es6/math/dice.js
<div class="border-gray">
    <p><button type="button" class="primary">roll the dice</button></p>
    <figure></figure>
</div>
Copied!
1
2
3
4

# Exercises

  • Open exercises/index.html and make the exercises under the tab ES6 > Math object
Last Updated: 4/22/2021, 10:20:29 PM