Skip to content

Commit 015497f

Browse files
committed
stretch goal on closure.js
1 parent 480bdf4 commit 015497f

File tree

2 files changed

+124
-29
lines changed

2 files changed

+124
-29
lines changed

assignments/closure.js

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,108 @@
11
// ==== Challenge 1: Write your own closure ====
2-
// Write a simple closure of your own creation. Keep it simple!
2+
// // Write a simple closure of your own creation. Keep it simple!
3+
// function kingdom(title) {
4+
// console.log(`Ye ole kingdom of ~${title}~`);
5+
// function town() {
6+
// console.log(`Welcome to the township of ${title}shire`);
7+
// }
8+
// town();
9+
// }
10+
// kingdom('Dragonscale');
311

412

513
// ==== Challenge 2: Create a counter function ====
6-
const counter = () => {
7-
// Return a function that when invoked increments and returns a counter variable.
8-
};
9-
// Example usage: const newCounter = counter();
10-
// newCounter(); // 1
11-
// newCounter(); // 2
14+
// // Return a function that when invoked increments and returns a counter variable.
15+
// const counter = () => {
16+
// let count = 8100;
17+
// // console.count(`Power level`);
18+
// // return count += 1;
19+
// return function() {
20+
// count += 10;
21+
// // console.count(``);
22+
// console.log(`Power level:`);
23+
// return count;
24+
25+
// }
26+
// };
27+
// // console.log(counter());
28+
// // console.log(counter());
29+
// // console.log(counter());
30+
// // console.log(counter());
31+
// const newCounter = counter();
32+
// console.log(newCounter());
33+
// console.log(newCounter());
34+
// console.log(newCounter());
35+
// console.log(newCounter());
36+
// console.log(`~~~ Almost there! ~~~`);
37+
38+
// // Function to increment counter ES5
39+
// // not really a counter though. console.log is tracking invocation,
40+
// // but the value of each previous iteration is not actually being stored (?)
41+
// let counter = 0;
42+
// function add() {
43+
// counter += 1;
44+
// console.log(counter);
45+
// }
46+
// add();
47+
// add();
48+
// add();
49+
1250

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

1553
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
16-
const counterFactory = () => {
17-
// Return an object that has two methods called `increment` and `decrement`.
18-
// `increment` should increment a counter variable in closure scope and return it.
19-
// `decrement` should decrement the counter variable and return it.
20-
};
54+
// Return an object that has two methods called `increment` and `decrement`.
55+
// `increment` should increment a counter variable in closure scope and return it.
56+
// `decrement` should decrement the counter variable and return it.
57+
// const counterFactory = (start, direction) => {
58+
// let count = start;
59+
// if (direction = 'up') {
60+
// return function() {
61+
// count += 1;
62+
// return count;
63+
// }
64+
// } else if (direction = 'down') {
65+
// return function() {
66+
// count -= 1;
67+
// return count;
68+
// }
69+
// } else {
70+
// return 'choose either up or down';
71+
// }
72+
73+
// };
74+
75+
// const countFact = counterFactory(0,up);
76+
// console.log(countFact());
77+
// console.log(countFact());
78+
// console.log(countFact());
79+
// console.log(countFact());
80+
81+
// // Ok, this isn't working. ... reads MDN ... meditates ...
82+
// // Challenge 3 Round 2
83+
const counter = (function() {
84+
let countDracula = 0;
85+
function changeBy(val) {
86+
countDracula += val;
87+
}
88+
return {
89+
increment: () => {
90+
changeBy(1);
91+
return `${countDracula} banana, ha. ha. ha.`;
92+
},
93+
decrement: () => {
94+
changeBy(-1);
95+
return `${countDracula} banana, ha. ha. ha.`;
96+
},
97+
value: function() {
98+
return `Count Dracula's current value is ${countDracula}`;
99+
}
100+
};
101+
}) ();
102+
103+
console.log(counter.increment());
104+
console.log(counter.decrement());
105+
console.log(counter.value());
106+
console.log(counter.increment());
107+
console.log(counter.increment());
108+
console.log(counter.increment());
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1+
// This is my answer to the same problem in JSI
2+
3+
4+
exampleArray = [1,2,3,4];
5+
16
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax
7+
let myFunction = () => {};
8+
9+
let anotherFunction = (param) => {
10+
return param;
11+
};
12+
anotherFunction('hello');
13+
214

3-
// let myFunction = function () {};
15+
let add = (param1, param2) => {
16+
return param1 + param2;
17+
};
18+
add(1,2);
419

5-
// let anotherFunction = function (param) {
6-
// return param;
7-
// };
820

9-
// let add = function (param1, param2) {
10-
// return param1 + param2;
11-
// };
12-
// add(1,2);
21+
let subtract = (param1, param2) => {
22+
return param1 - param2;
23+
};
24+
subtract(1,2);
1325

14-
// let subtract = function (param1, param2) {
15-
// return param1 - param2;
16-
// };
17-
// subtract(1,2);
1826

19-
// exampleArray = [1,2,3,4];
20-
// const triple = exampleArray.map(function (num) {
21-
// return num * 3;
22-
// });
23-
// console.log(triple);
27+
const triple = exampleArray.map((num) => {
28+
return num * 3;
29+
});
30+
console.log(triple);

0 commit comments

Comments
 (0)