Skip to content

Commit

Permalink
Additional solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
katesmatthews committed Oct 18, 2019
1 parent c4baf8b commit 20de7d4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
27 changes: 27 additions & 0 deletions async.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,30 @@ function everyXsecsForYsecs(func, interval, duration) {
// console.log('This is the end!');
// }
// everyXsecsForYsecs(theEnd, 2, 20); // should invoke theEnd function every 2 seconds, for 20 seconds): This is the end!


/* CHALLENGE 7 */

function delayCounter(target, wait) {
let intervalId;
let counter = 0;
return function inner() {
if (counter === 0) {
counter++;
intervalId = setInterval(() => console.log(inner()), wait);
} else if (counter === target) {
clearInterval(intervalId);
return counter;
} else {
return counter++;
}
}
}

// UNCOMMENT THESE TO TEST YOUR WORK!
// const countLogger = delayCounter(3, 1000)
// countLogger();
// After 1 second, log 1
// After 2 seconds, log 2
// After 3 seconds, log 3

18 changes: 18 additions & 0 deletions callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,21 @@ function multiMap(arrVals, arrCallbacks) {
// 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; }]));
// should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] }


//Extension 7
function objectFilter(obj, callback) {
const newObj = {};
for (let key in obj) {
if (callback(key) === obj[key]) {
newObj[key] = callback(key);
}
}
return newObj;
}

// const cities = {
// London: 'LONDON',
// LA: 'Los Angeles',
// Paris: 'PARIS',
// };
// console.log(objectFilter(cities, city => city.toUpperCase())) // Should log { London: 'LONDON', Paris: 'PARIS'}
16 changes: 16 additions & 0 deletions closures.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,19 @@ const afterCalled = after(3, called);
function delay(func, wait, ...args) {
setTimeout(() => func(...args), wait);
}

// Challenge 8
function rollCall(names) {
return () => {
if (!names.length) return console.log('Everyone accounted for');
console.log(names.shift());
}
}

// UNCOMMENT THESE TO TEST YOUR WORK!
// const rollCaller = rollCall(['Victoria', 'Juan', 'Ruth'])
// rollCaller() // -> Should log 'Victoria'
// rollCaller() // -> Should log 'Juan'
// rollCaller() // -> Should log 'Ruth'
// rollCaller() // -> Should log 'Everyone accounted for'

26 changes: 26 additions & 0 deletions oop.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,29 @@ const adminFromFactory = adminFactory("Eva", 5);
// /********* Uncomment these lines to test your work! *********/
adminFromFactory.sayType() // -> Logs "I am a Admin"
adminFromFactory.sharePublicMessage() // -> Logs "Welcome users!"


/****************************************************************
EXTENSION: MIXINS
****************************************************************/

class Dog {
constructor() {
this.legs = 4;
}
speak() {
console.log('Woof!');
}
}

const robotSkillsMixin = {
skin: 'metal',
speak: function () { console.log(`I have ${this.legs} legs and am made of ${this.skin}`) },
}

let robotFido = new Dog();

robotFido = Object.assign(robotFido, robotSkillsMixin);

// /********* Uncomment these lines to test your work! *********/
robotFido.speak() // -> Logs "I am made of metal"

0 comments on commit 20de7d4

Please sign in to comment.