Skip to content

Commit c819b54

Browse files
Task 4: closures.js completed
1 parent ff5909d commit c819b54

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

‎assignments/closure.js‎

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

6+
const sayHi = () => {
7+
console.log(`Hi ${firstName}!`);
8+
}
9+
return sayHi;
10+
}
11+
12+
const hiKendra = name("Kendra");
13+
const hiJohn = name("John");
14+
hiKendra();
15+
hiJohn();
416

517
// ==== Challenge 2: Create a counter function ====
618
const counter = () => {
719
// Return a function that when invoked increments and returns a counter variable.
20+
let count = 0;
21+
22+
const increment = () => {
23+
console.log(++count);
24+
};
25+
return increment;
826
};
9-
// Example usage: const newCounter = counter();
10-
// newCounter(); // 1
11-
// newCounter(); // 2
27+
// Example usage:
28+
const newCounter = counter();
29+
newCounter(); // 1
30+
newCounter(); // 2
1231

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

0 commit comments

Comments
 (0)