Skip to content

Commit 4b42a2c

Browse files
committed
Added challenge 3 stretch goal
1 parent 872ff7d commit 4b42a2c

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

assignments/closure.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ console.log(addTwoAndFour(20));
1515

1616

1717
// ==== Challenge 2: Implement a "counter maker" function ====
18-
const counterMaker = (limit) => {
18+
const counterMaker = () => {
1919
// IMPLEMENTATION OF counterMaker:
2020
// 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`!
2121
// 2- Declare a function `counter`. It should increment and return `count`.
@@ -29,12 +29,8 @@ const counterMaker = (limit) => {
2929
}
3030

3131
let myCounter = counterMaker();
32-
let myCounter2 = counterMaker();
32+
console.log(myCounter());
3333

34-
alert( myCounter() ); // 0
35-
alert( myCounter() ); // 1
36-
37-
alert( myCounter2() ); // 0 (independent)
3834

3935
// Example usage: const myCounter = counterMaker();
4036
// myCounter(); // 1
@@ -43,7 +39,15 @@ alert( myCounter2() ); // 0 (independent)
4339
// ==== Challenge 3: Make `counterMaker` more sophisticated ====
4440
// It should have a `limit` parameter. Any counters we make with `counterMaker`
4541
// will refuse to go over the limit, and start back at 1.
46-
42+
const counterMaker2 = (limit) => {
43+
let count = 0;
44+
return function counter2(){
45+
count = (count % limit) + 1
46+
return count
47+
}
48+
};
49+
const myCounter2 = counterMaker2(10)
50+
console.log(myCounter2());
4751
// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
4852
const counterFactory = () => {
4953
// Return an object that has two methods called `increment` and `decrement`.

0 commit comments

Comments
 (0)