-
Notifications
You must be signed in to change notification settings - Fork 1
/
hFunction.js
115 lines (100 loc) · 2.59 KB
/
hFunction.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function countdown(num) {
let startNumber = num;
let fn = function () {
startNumber--;
if (startNumber === 0) {
clearInterval(timerId);
console.log("DONE!");
} else {
console.log(startNumber);
}
};
let timerId = setInterval(fn, 1000);
}
// countdown (5);
//2
function randomGame(){
function counter(){
let count = 0;
return function(){
return ++count
}
}
let gameCount = counter();
function fn(){
var randomNum = Math.random();
if(randomNum > 0.75){
console.log(gameCount());
clearInterval(timerId);
} else {
gameCount()
}
}
let timerId = setInterval(fn, 1000);
}
// console.log(randomGame())
//3
function isEven(num) {
return num % 2 === 0 ? true : false;
}
// console.log(isEven (2))
//4
function isOdd(num) {
return num % 2 !== 0 ? true : false;
}
// console.log(isOdd (14))
//5
function isPrime(n){
for (let i = 2; i <= Math.sqrt(n); i++) {
if(n % i === 0){
return false;
}
};
if(n !== Math.round(n)){
return false;
} else if(n < 2){
return false;
} else {
return true;
}
}
// console.log(isPrime(8))
//6
function numberFact(num,callback){
return callback(num)
}
// console.log(numberFact(59, isEven))
//7
function find (array, callback){
return array.find (function (value){
if (callback(value))
return value
})
}
let result = find ([8,11,4,27], function(val){
return val >= 10
})
// console.log (result)
//8
function findIndex (array, callback){
return array.findIndex (function (value){
if (callback(value))
return value
})
}
// let result = findIndex ([8,11,4,27], function(val){
// return val >= 10
// })
// console.log (result)
//9
function specialMultiply(a,b){
if(arguments.length === 1){
return function(b){
return a*b;
}
}
return a*b;
}
// console.log(specialMultiply(3,4)) // 12
// console.log(specialMultiply(3)(4)); // 12
// console.log(specialMultiply(3));