forked from delventhalz/paleo.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.Math.js
66 lines (46 loc) · 1.6 KB
/
01.Math.js
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
65
66
/* * * * * * * * * * * * * * * * * * * * * *
* CAVEMAN JS PT 1: THE MATH OBJECT *
* * * * * * * * * * * * * * * * * * * * * */
// The Math object has all sorts of useful methods that JavaScript
// programmers use every day, like `Math.sqrt` (square root) or
// `Math.random` (generates random number). But we're caveman coders
// so we're going to go without, and implement these methods manually.
/** Math.floor **/
// Our first function rounds a number down to the next whole number.
// To get you in the swing of things, I implemented it for you.
var floor = function(number) {
var r = number % 1;
return number < 0 ? number - (!r ? 0 : 1 + r) : number - r;
};
/** Math.ceiling **/
// This one rounds up. Your solution may differ from mine.
// Just make sure it works.
var ceiling = function(number) {
var r = number % 1;
if (r === 0) {
return number;
}
if (number < 0) {
return number - r;
}
return number + (1 - r);
};
/** Math.abs **/
// This one simply returns the absolute value of a number.
var abs = function(number) {
};
/** Math.pow **/
// JavaScript doesn't have an exponent operator, so you need `Math.pow`
// to raise a number to a power. Too bad you can't use that.
var pow = function(base, exponent) {
};
/** Math.max **/
// Normally this compares any number of numbers and returns the
// largest. Let's make a version that just compares two.
var max = function(x, y) {
};
/** Math.min **/
// I bet you can guess what this one is suppossed to do. This time,
// use the `arguments` keyword so that you can compare more than two.
var min = function() {
};