Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 196 additions & 22 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"regenerator-runtime": "^0.10.3"
},
"dependencies": {
"eslint-config-airbnb": "^14.1.0"
"eslint-config-airbnb": "^14.1.0",
"underscore": "^1.8.3"
}
}
11 changes: 6 additions & 5 deletions src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ const map = (elements, cb) => {

const reduce = (elements, cb, startingValue) => {
if (startingValue === undefined) {
startingValue = elements[0];
startingValue = elements.shift();
}
let sum = 0;
for (let i = 0; i < elements.length; i++) {
sum += cb(startingValue, elements[i]);

while (elements.length > 0) {
startingValue = cb(startingValue, elements.shift());
}
return sum;
return startingValue;

// Do NOT use .reduce, to complete this function.
// Combine all elements into a single value going from left to right.
// Elements will be passed one by one into `cb` along with the `startingValue`.
Expand Down
4 changes: 3 additions & 1 deletion src/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const multiplyNums = (x, y, cb) => {
const contains = (item, list, cb) => {
if (list.indexOf(item) >= 0) {
cb(true);
} cb(false);
} else {
cb(false);
}
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
};
Expand Down
34 changes: 34 additions & 0 deletions src/closure.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
// Complete the following functions.

const counter = () => {
let value = 0;
return () => {
return ++value;
};
// Return a function that when invoked increments and returns a counter variable.
// Example: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
};

const counterFactory = () => {
let value = 0;
return {
increment: () => {
return ++value;
},
decrement: () => {
return --value;
}
};
// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
};

const limitFunctionCallCount = (cb, n) => {
let value = 0;
return (par1, par2, par3) => {
if (value < n) {
value++;
return cb(par1, par2, par3);
}
if (value === n) {
return null;
}
};
// Should return a function that invokes `cb`.
// The returned function should only allow `cb` to be invoked `n` times.
};

/* STRETCH PROBLEM */

const cacheFunction = (cb) => {
const myObj = {};
const func = cb;
return (arg) => {
if (arg in myObj) {
return myObj[arg];
}
if (myObj[arg] === undefined) {
myObj[arg] = func(arg);
return myObj[arg];
}
};
// Should return a funciton that invokes `cb`.
// A cache (object) should be kept in closure scope.
// The cache should keep track of all arguments have been used to invoke this function.
Expand Down
35 changes: 30 additions & 5 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,55 @@ const values = (obj) => {
};

const mapObject = (obj, cb) => {
return Object.mapObject(obj, function(val, key => cb(val)));
const prop = Object.keys(obj);
const ties = Object.values(obj);
const myObj = {};
for (let i = 0; i < prop.length; i++) {
myObj[prop[i]] = cb(ties[i]);
}
return myObj;
// Like map for arrays, but for objects. Transform the value of each property in turn.
// http://underscorejs.org/#mapObject
};

const pairs = (obj) => {
return Object.pairs(obj);
const prop = Object.keys(obj);
const ties = Object.values(obj);
const arr = [];
for (let i = 0; i < prop.length; i++) {
arr.push([prop[i], ties[i]]);
}
return arr;
// Convert an object into a list of [key, value] pairs.
// http://underscorejs.org/#pairs
};

/* STRETCH PROBLEMS */

const invert = (obj) => {
return Object.invert(obj);
const prop = Object.keys(obj);
const ties = Object.values(obj);
const myObj = {};
for (let i = 0; i < prop.length; i++) {
myObj[ties[i]] = (prop[i]);
}
return myObj;
// Returns a copy of the object where the keys have become the values and the values the keys.
// Assume that all of the object's values will be unique and string serializable.
// http://underscorejs.org/#invert
};

const defaults = (obj, defaultProps) => {
return Object.defaults(obj, defaultProps);
const propDef = Object.keys(defaultProps);
const tiesDef = Object.values(defaultProps);
for (let i = 0; i < propDef.length; i++) {
if (obj[propDef[i]] === undefined) {
obj[propDef[i]] = tiesDef[i];
}
}
return obj;
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
// Return `obj`.
// Return `obj`.r
// http://underscorejs.org/#defaults
};

Expand Down
Loading