-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIIFE.js
More file actions
61 lines (50 loc) · 1.13 KB
/
IIFE.js
File metadata and controls
61 lines (50 loc) · 1.13 KB
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
// 2. Edit the code from problem one so it executes without error.
// (function() {
// console.log("Sometimes, syntax isn't intuitive!")
// })();
// 3. The code below throws an error:
// What kind of problem does this error highlight?
// Use an IIFE to address it, so that code runs without error.
// var sum = 0;
// var numbers;
// sum += 10;
// sum += 31;
// numbers = [1, 7, -3, 3];
// function sum(arr) {
// return arr.reduce(function(sum, number) {
// sum += number;
// return sum;
// }, 0);
// }
// (function(arr) {
// return arr.reduce((sum, number) => sum += number), 0);
// })(numbers);
//sum += sum(numbers); // ?
// sum += (function(arr) {
// return arr.reduce((sum, number) => sum += number, 0);
// })(numbers);
// console.log(sum);
// 4. Consider the output below:
// Implement a function countdown that uses an IIFE to generate the desired output.
// countdown(7);
// 7
// 6
// 5
// 4
// 3
// 2
// 1
// 0
// Done!
function countdown(count) {
(function logCount(n){
if (n >= 0) {
console.log(n);
n -= 1;
logCount(n);
} else {
console.log('Done!');
}
})(count);
}
countdown(7);