forked from TrainingByPackt/Professional-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.js
More file actions
36 lines (31 loc) · 787 Bytes
/
number.js
File metadata and controls
36 lines (31 loc) · 787 Bytes
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
Number.prototype.double = function () {
return this.valueOf()*2;
}
Number.prototype.square = function () {
return this.valueOf()*this.valueOf();
}
Number.prototype.fibonacci = function () {
function iterator(a, b, n) {
return n == 0n ? b : iterator((a+b), a, (n-1n))
}
function fibonacci(n) {
n = BigInt(n);
return iterator(1n, 0n, n);
}
return fibonacci(this.valueOf());
}
Number.prototype.factorial = function () {
factorial = (n) => {
n = BigInt(n);
return (n>1) ? n * factorial(n-1n) : n;
}
return factorial(this.valueOf());
}
let n = 100;
console.log(
"for number " + n +"\n",
"double is " + n.double() + "\n",
"square is " + n.square() + "\n",
"fibonacci is " + n.fibonacci() + "\n",
"factorial is " + n.factorial() + "\n"
);