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 ( ) ) ;
0 commit comments