-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path24-mathObjects.html
64 lines (47 loc) · 2.25 KB
/
24-mathObjects.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Demo</title>
</head>
<body>
<script type="text/javascript">
// Working with math objects
/*
Javascript does math!
*/
document.write(Math.PI + '<br>'); // JS KNOWS PI!
document.write(Math.E + '<br>'); // Euler
// That's fun, but let's make this interactive
var number = prompt('Enter a number', '');
var answer = Math.sqrt(number);
// Let's test to see if this is a number or not and send the correct reply
if (isNaN(answer)) {
document.write('That is not a number dude!' + '<br>');
} else {
document.write('The square root of ' + number + ' is ' + answer + '<br>');
};
// JavaScript provides eight mathematical constants that can be accessed from the Math object. Mathematical constant = an interesting number.
// Other things we can do with math are ...
// Round numbers
var roundNumber = Math.round(answer);
document.write('Rounded up is ' + roundNumber + '<br>');
// Max number
var topNumber = roundNumber;
var bottomNumber = roundNumber / 2;
var maxNumber = Math.max(topNumber, bottomNumber);
document.write('Top number is ' + topNumber + ', low number is ' + bottomNumber + ', so your max number is ' + maxNumber + '<br>');
// Min number
var topNumber = roundNumber;
var bottomNumber = roundNumber / 2;
var minNumber = Math.min(topNumber, bottomNumber);
document.write('Top number is ' + topNumber + ', low number is ' + bottomNumber + ', so your min number is ' + minNumber + '<br>');
// Random numbers
var randomNumber = Math.ceil(Math.random() * maxNumber);
document.write('Your random number is ' + randomNumber + '<br>');
// For a full list of math methods
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
</script>
</body>
</html>