Skip to content

Commit 73d8a9a

Browse files
Javascript-II assignment
1 parent d05f670 commit 73d8a9a

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

‎assignments/closure.js‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3+
function top() {
4+
let topVariable = "I am in top()";
5+
//console.log(`Not able to access bottom() variable : ${bottomVariable}`); // will get an error bottomVariable is not defined.
6+
function middle() {
7+
let middleVariable = "I am in middle()";
8+
function bottom() {
9+
let bottomVariable = "I am in bottom()";
10+
11+
console.log(`bottom() :-- able to access top() and middle() variable because of closure \n ${topVariable} \n ${middleVariable}`);
12+
}
13+
bottom();
14+
}
15+
middle();
16+
}
17+
top();
318

419

520
// ==== Challenge 2: Create a counter function ====
6-
const counter = () => {
21+
const counter = (num = 1) => {
22+
let count = (num) => {return num += 1; } count();
723
// Return a function that when invoked increments and returns a counter variable.
824
};
9-
// Example usage: const newCounter = counter();
10-
// newCounter(); // 1
11-
// newCounter(); // 2
25+
// Example usage:
26+
const newCounter = counter();
27+
console.log(counter()); // should return 2.
28+
1229

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

0 commit comments

Comments
 (0)