Skip to content

Commit b9ecc98

Browse files
committed
completed the closure.js challenges
1 parent 6667665 commit b9ecc98

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎assignments/closure.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
33

4+
function Healthpoints(hp) {
5+
const hpValue = hp;
6+
console.log(`You have ${hp} hp.`);
7+
//debugger;
8+
9+
function takeDamage() {
10+
const tookdmg = hp-1;
11+
console.log(`${tookdmg} you know have ${hp} left`);
12+
//debugger;
13+
14+
function faint() {
15+
const fainted = "You have fainted"
16+
console.log(`${fainted} because your hp was ${hp} but is now ${tookdmg}`);
17+
//debugger;
18+
}
19+
20+
faint();
21+
}// faint
22+
23+
takeDamage();
24+
}// takeDamage
25+
Healthpoints(1);
426

527
// ==== Challenge 2: Create a counter function ====
28+
let num = 0;
629
const counter = () => {
730
// Return a function that when invoked increments and returns a counter variable.
31+
num++
32+
return num;
833
};
34+
35+
console.log(counter());
36+
console.log(counter());
937
// Example usage: const newCounter = counter();
1038
// newCounter(); // 1
1139
// newCounter(); // 2

0 commit comments

Comments
 (0)