9.1  Java Script - Math Object

Index Code Tester

OBJECTIVE

DISCUSSION

Math Object 

alert(Math.cos(Math.PI))  //trigonometry methods require angle in radians, not degrees
x = Math.cos(Math.PI)
alert(Math.abs(x))
y = Math.sin(Math.PI)
alert(Math.floor(y))
alert(Math.pow(3, 4))
  

Other Functions and Operators (Not part of Math object) 

var x = "a";
while (isNaN(x)) {
   x = prompt("Enter a number")
}
alert(x % 2)   

 

EXERCISES

1.  List the Math methods and properties.

2.  Write a function that accepts an angle in degrees and returns the sin of the angle.  Note that you must convert degrees to radians by multiplying degrees by PI and dividing by 180. 

3.  Write a program to test your function from #2.  The program should request the user enter a number in degrees.  If the user enters a number, the sin of the number should be displayed; otherwise the user should be asked to enter a number. To help determine if your function is correct, the sin of 30 degrees is 0.5. 

4.  What is the difference between the following three methods: round(), ceil(), and floor()?  Create a program to demonstrate these differences.

5.  The method "parseInt()" is not a Math method. What does parseInt() do? 

6.  Write a function that determines if a number is prime or not.  The function should accept a number and return true if the number is prime or false if the number is not prime.  A prime number is a number that is divisible only by itself and one.  For example, 10 is NOT prime since it si divisible by 2 and 5.  The number 5 IS prime since it is divisible by 1 and 5 only.

7.  Create a program to determine all prime number between 1 and 1000. 

 

 

LINKS AND HELP

http://www.w3schools.com/js/js_math.asp