Skip to content

Commit 00e9864

Browse files
committed
Completed closures. I thought they weren't working but debugger shows they are.
1 parent e7a7aed commit 00e9864

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

assignments/closure.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3+
const person = () => {
4+
let name = 'Jen';
35

6+
const speak = () => {
7+
console.log( `${name} says, 'Hello!'` );
8+
}
9+
return speak;
10+
}
11+
const newPerson = person();
12+
newPerson();
413

514
// ==== Challenge 2: Create a counter function ====
615
const counter = () => {
716
// Return a function that when invoked increments and returns a counter variable.
8-
let count = 0;
9-
function add() {
17+
var count = 0;
18+
const add = () => {
1019
count += 1;
20+
debugger;
1121
}
1222
return add;
1323
};
14-
// Example usage: const newCounter = counter();
15-
// newCounter(); // 1
16-
// newCounter(); // 2
24+
const newCounter = counter();
25+
newCounter(); // 1
26+
newCounter(); // 2
1727

1828
/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */
1929

0 commit comments

Comments
 (0)