-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusingclosures.js
53 lines (38 loc) · 1.15 KB
/
usingclosures.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
// var rollDie = function () {
// return Math.floor(1 + Math.random() * 6);
// }
// console.log(rollDie(1));
function makeLoadedDie() {
var list = [5, 4, 6, 1, 6, 4, 2, 3, 3, 5];
return function() {
var result = list.shift()
var newList = list.push(result)
return result
}
}
var rollLoadedDie = makeLoadedDie();
console.log(rollLoadedDie()); // 5
console.log(rollLoadedDie()); // 4
console.log(rollLoadedDie()); // 6
console.log(rollLoadedDie()); // 1
console.log(rollLoadedDie()); // 6
console.log(rollLoadedDie()); // 4
var countdownGenerator = function (x) {
return function () {
if (x > 0) {
console.log('T-minus ' + x + '...')
} else if (x === 0) {
console.log('Blast off!')
} else {
console.log('Rockets already gone, bub!')
}
x--
}
};
var countdown = countdownGenerator(3);
countdown(); // T-minus 3...
countdown(); // T-minus 2...
countdown(); // T-minus 1...
countdown(); // Blast Off!
countdown(); // Rockets already gone, bub!
countdown(); // Rockets already gone, bub!