forked from DevMountain/javascript-1-afternoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosures.js
127 lines (70 loc) · 2.84 KB
/
closures.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
116
117
118
119
120
121
122
123
124
125
//Once you complete a problem, open up Chrome and check the answer in the console.
var outer = function(){
var name = 'Tyler';
return function(){
return 'The original name was ' + name;
}
}
//Above you're given a function that returns another function which has a closure over the name variable.
//Invoke outer saving the return value into another variable called 'inner'.
//Code Here
//Once you do that, invoke inner.
//Code Here
//Next problem
var callFriend = function(){
var friend = 'Jake';
function callF(number){
return 'Calling ' + friend + ' at ' + number;
}
return callF;
};
//Above you're given a callFriend function that returns another function.
//Do what you need to do in order to call your function and get 'Calling Jake at 435-215-9248' in your console.
//Code Here
//Next Problem
/*
Write a function called makeCounter that makes the following code work properly.
*/
//Code Here
var count = makeCounter();
count(); // 1
count(); // 2
count(); // 3
count(); // 4
//Next Problem
/*
Write a function named codeLove that returns the string 'I love code'. Write a second function named codeFriend that accepts the first function as it's first parameter. The second function should return a new third function. Store the third function in a variable, codeEcho which, when invoked, invokes the first, original function that was passed in, but will only ever do so once (returns null after first invocation).
*/
//Code Here
//Next Problem
/*
Now, similar to the last problem, write a function called 'fnCounter' that accepts two parameters. The first parameter will be an anonymous function and the second parameter, 'N', will be a number. Now, in 'fnCounter', allow the anonymous funciton to be invoked 'N' number of times. After it's been invoked 'N' number of times, return 'STOP'.
*/
//Next Problem
/*
var counter = function(){
for (var i=1; i<=5; i++) {
setTimeout( function timer(){
console.log( i );
}, i*1000 );
}
};
Above you have a function named counter. Examine the function (without running the code) then below write what you expect to happen when the funciton is invoked. *Hint: setTimeout calls a function or evaluates an expression after a specified number of milliseconds.
//Answer Here
Now, run the function in your console and note what happpens.
Was your answer right or wrong?
//Answer Here
Fix the counter function so that it works the way you expect it to work. (logging 1 then 2 then 3, etc) (Note: No unit test for this one because of the timeout)
*/
//Code Here
//Next Problem
/*
Make the following code work
funcArray[0]() //0
funcArray[1]() //1
funcArray[2]() //2
funcArray[3]() //3
funcArray[4]() //4
funcArray[5]() //5
*Hint: Don't let this fool you. Break down what's really happening here.
*/