-
Notifications
You must be signed in to change notification settings - Fork 0
/
closure.js
52 lines (47 loc) · 1.06 KB
/
closure.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
// function makeCounterLogger(num1) {
// return function(num2) {
// let i;
// if (num1 < num2) {
// for (let i = num1; i <= num2; i += 1) {
// console.log(i);
// }
// } else if (num1 > num2) {
// for (let i = num1; i >= num2; i -= 1) {
// console.log(i);
// }
// }
// }
// }
// let countlog = makeCounterLogger(5);
// countlog(8);
// console.log('');
// countlog(2);
function makeList() {
let todos = [];
return function (todo) {
if (todo === undefined) {
if (todos.length === 0) {
console.log('This list is empty.');
} else {
todos.forEach(item => console.log(item));
}
} else if (todos.includes(todo)) {
let index = todos.indexOf(todo);
todos.splice(index, 1);
console.log(todo + ' removed!');
} else {
todos.push(todo);
console.log(todo + ' added!');
}
}
}
let list = makeList();
list();
console.log('');
list('make breakfast');
list('read book');
console.log('');
list();
console.log('');
list('make breakfast');
list();