-
Notifications
You must be signed in to change notification settings - Fork 0
/
HigherOrderFunctions.js
56 lines (45 loc) · 1.35 KB
/
HigherOrderFunctions.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
/*
Higher order functions
-------------------------------------------------
i) they can take functions as an argument
ii) or return a function as answer.
-------------------------------------------------
*/
// ======================
// FUNCTIONS AS ARGUMENTS
// ======================
function callTwice(func) {//remember to remove()
func();
func();
}
function rollDie() {
const roll = Math.floor(Math.random() * 6) + 1;
console.log(roll)
}
callTwice(rollDie)
// ====================
// RETURNING FUNCTIONS
// ====================
function makeMysteryFunc() {
const rand = Math.random(); //depending on this random no, we are returning on of the below functions
if (rand > 0.5) {
return function () {
console.log("CONGRATS, I AM A GOOD FUNCTION!")
console.log("YOU WIN A MILLION DOLLARS!!")
}
} else {
return function () {
alert("YOU HAVE BEEN INFECTED BY A COMPUTER VIRUS!")
alert("STOP TRYING TO CLOSE THIS WINDOW!")
alert("STOP TRYING TO CLOSE THIS WINDOW!")
alert("STOP TRYING TO CLOSE THIS WINDOW!")
alert("STOP TRYING TO CLOSE THIS WINDOW!")
alert("STOP TRYING TO CLOSE THIS WINDOW!")
}
}
}
function makeBetweenFunc(min, max) {
return function (num) {
return num >= min && num <= max;
}
}