Skip to content

Commit 20de7d4

Browse files
committed
Additional solutions
1 parent c4baf8b commit 20de7d4

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

async.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,30 @@ function everyXsecsForYsecs(func, interval, duration) {
6161
// console.log('This is the end!');
6262
// }
6363
// everyXsecsForYsecs(theEnd, 2, 20); // should invoke theEnd function every 2 seconds, for 20 seconds): This is the end!
64+
65+
66+
/* CHALLENGE 7 */
67+
68+
function delayCounter(target, wait) {
69+
let intervalId;
70+
let counter = 0;
71+
return function inner() {
72+
if (counter === 0) {
73+
counter++;
74+
intervalId = setInterval(() => console.log(inner()), wait);
75+
} else if (counter === target) {
76+
clearInterval(intervalId);
77+
return counter;
78+
} else {
79+
return counter++;
80+
}
81+
}
82+
}
83+
84+
// UNCOMMENT THESE TO TEST YOUR WORK!
85+
// const countLogger = delayCounter(3, 1000)
86+
// countLogger();
87+
// After 1 second, log 1
88+
// After 2 seconds, log 2
89+
// After 3 seconds, log 3
90+

callbacks.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,21 @@ function multiMap(arrVals, arrCallbacks) {
112112
// console.log(multiMap(['catfood', 'glue', 'beer'], [function(str) { return str.toUpperCase(); }, function(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }, function(str) { return str + str; }]));
113113
// should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] }
114114

115+
116+
//Extension 7
117+
function objectFilter(obj, callback) {
118+
const newObj = {};
119+
for (let key in obj) {
120+
if (callback(key) === obj[key]) {
121+
newObj[key] = callback(key);
122+
}
123+
}
124+
return newObj;
125+
}
126+
127+
// const cities = {
128+
// London: 'LONDON',
129+
// LA: 'Los Angeles',
130+
// Paris: 'PARIS',
131+
// };
132+
// console.log(objectFilter(cities, city => city.toUpperCase())) // Should log { London: 'LONDON', Paris: 'PARIS'}

closures.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,19 @@ const afterCalled = after(3, called);
112112
function delay(func, wait, ...args) {
113113
setTimeout(() => func(...args), wait);
114114
}
115+
116+
// Challenge 8
117+
function rollCall(names) {
118+
return () => {
119+
if (!names.length) return console.log('Everyone accounted for');
120+
console.log(names.shift());
121+
}
122+
}
123+
124+
// UNCOMMENT THESE TO TEST YOUR WORK!
125+
// const rollCaller = rollCall(['Victoria', 'Juan', 'Ruth'])
126+
// rollCaller() // -> Should log 'Victoria'
127+
// rollCaller() // -> Should log 'Juan'
128+
// rollCaller() // -> Should log 'Ruth'
129+
// rollCaller() // -> Should log 'Everyone accounted for'
130+

oop.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,29 @@ const adminFromFactory = adminFactory("Eva", 5);
192192
// /********* Uncomment these lines to test your work! *********/
193193
adminFromFactory.sayType() // -> Logs "I am a Admin"
194194
adminFromFactory.sharePublicMessage() // -> Logs "Welcome users!"
195+
196+
197+
/****************************************************************
198+
EXTENSION: MIXINS
199+
****************************************************************/
200+
201+
class Dog {
202+
constructor() {
203+
this.legs = 4;
204+
}
205+
speak() {
206+
console.log('Woof!');
207+
}
208+
}
209+
210+
const robotSkillsMixin = {
211+
skin: 'metal',
212+
speak: function () { console.log(`I have ${this.legs} legs and am made of ${this.skin}`) },
213+
}
214+
215+
let robotFido = new Dog();
216+
217+
robotFido = Object.assign(robotFido, robotSkillsMixin);
218+
219+
// /********* Uncomment these lines to test your work! *********/
220+
robotFido.speak() // -> Logs "I am made of metal"

0 commit comments

Comments
 (0)