-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursion
61 lines (47 loc) · 1.04 KB
/
recursion
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
function factorial(num){
var sum = 1;
while(num>0){
sum *= num;
num -= 1;
}
return sum;
}
function factorialRecursion(num){
var sum = 1;
if(num === 0){
return sum;
}
return sum *= factorialRecursion(num-1);
}
function fib(num){
if(num < 2){
return 1;
}
return (fib(n-1) + fib(n-2));
}
function type(value){
Object.prototype.toString.call(value).slice(8,-1);
}
function stringify(object){
type(object) === 'Function'? object + '';
type(object) === 'Null'? object + '';
type(object) === 'Boolean'? object + '';
type(object) === 'Number'? object + '';
type(object) === 'Undefined'? object + '';
type(object) === 'String'? '"' + object + '"';
if(type(object) === 'Array'){
return '[' + object.map(function(obj){
return stringify(obj);
}).join(',') +']';
}
if(type(object) === 'Object'){
var container = [];
Object.keys(object).forEach(function(key)){
var value = stringify(object[key]);
if(value !== null){
container.push('"' + key + '":' + value);
}
};
return '{' + container.join(',') + '}';
}
}