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
Binary file added .DS_Store
Binary file not shown.
454 changes: 45 additions & 409 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",
"yarn": "^1.5.1"
}
}
Binary file added src/.DS_Store
Binary file not shown.
30 changes: 30 additions & 0 deletions src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ const each = (elements, cb) => {
// This only needs to work with arrays.
// You should also pass the index into `cb` as the second argument
// based off http://underscorejs.org/#each
for (let i = 0; i < elements.length; i++) {
cb(elements[i], i);
}
};

const map = (elements, cb) => {
// Do NOT use .map, to complete this function.
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
// Return the new array.
const myArr = [];
for (let i = 0; i < elements.length; i++) {
myArr.push(cb(elements[i], i));
}
return myArr;
};

const reduce = (elements, cb, startingValue) => {
Expand All @@ -28,26 +36,48 @@ const reduce = (elements, cb, startingValue) => {
// Elements will be passed one by one into `cb` along with the `startingValue`.
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
const elementsCopy = elements.slice();
let memo = startingValue || elementsCopy.shift();
each(elementsCopy, (item) => {
memo = cb(memo, item);
});
return memo;
};

const find = (elements, cb) => {
// Do NOT use .includes, to complete this function.
// Look through each value in `elements` and pass each element to `cb`.
// If `cb` returns `true` then return that element.
// Return `undefined` if no elements pass the truth test.
for (let i = 0; i < elements.length; i++) {
if (cb(elements[i])) {
return elements[i];
}
}
return undefined;
};

const filter = (elements, cb) => {
// Do NOT use .filter, to complete this function.
// Similar to `find` but you will return an array of all elements that passed the truth test
// Return an empty array if no elements pass the truth test
const newArray = [];
for (let i = 0; i < elements.length; i++) {
if (cb(elements[i])) {
newArray.push(elements[i]);
}
}
return newArray;
};

/* STRETCH PROBLEM */

const flatten = (elements) => {
// Flattens a nested array (the nesting can be to any depth).
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
return elements.reduce((memo, num) => {
return memo.concat(Array.isArray(num) ? flatten(num) : num);
}, []);
};

/* eslint-enable no-unused-vars, max-len */
Expand Down
14 changes: 13 additions & 1 deletion src/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
const firstItem = (arr, cb) => {
// firstItem passes the first item of the given array to the callback function.
cb(arr[0]);
};

const getLength = (arr, cb) => {
// getLength passes the length of the array into the callback.
cb(arr.length);
};

const last = (arr, cb) => {
// last passes the last item of the array into the callback.
cb(arr[arr.length - 1]);
};

const sumNums = (x, y, cb) => {
// sumNums adds two numbers (x, y) and passes the result to the callback.
cb(x + y);
};

const multiplyNums = (x, y, cb) => {
// multiplyNums multiplies two numbers and passes the result to the callback.
cb(x * y);
};

const contains = (item, list, cb) => {
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
cb(list.includes(item));
};

/* STRETCH PROBLEM */
Expand All @@ -29,8 +35,14 @@ const removeDuplicates = (array, cb) => {
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
const arr = [];
for (let i = 0; i < array.length; i++) {
if (!arr.includes(array[i])) {
arr.push(array[i]);
}
}
cb(arr);
};

/* eslint-enable */
module.exports = {
firstItem,
Expand Down
57 changes: 42 additions & 15 deletions src/closure.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
// Complete the following functions.

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

const counterFactory = () => {
// 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.
// 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.
let count1 = 0;
let count2 = 0;
const newObj = {
increment: () => {
count1++;
return count1;
},
decrement: () => {
count2--;
return count2;
}
};
return newObj;
};

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

/* STRETCH PROBLEM */

const cacheFunction = (cb) => {
// 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.
// If the returned function is invoked with arguments that it has already seen
// then it should return the cached result and not invoke `cb` again.
// `cb` should only ever be invoked once for a given set of arguments.
// 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.
// If the returned function is invoked with arguments that it has already seen
// then it should return the cached result and not invoke `cb` again.
// `cb` should only ever be invoked once for a given set of arguments.
};

/* eslint-enable no-unused-vars */
Expand Down
21 changes: 21 additions & 0 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,43 @@ const keys = (obj) => {
// Retrieve all the names of the object's properties.
// Return the keys as strings in an array.
// Based on http://underscorejs.org/#keys
return Object.keys(obj);
};

const values = (obj) => {
// Return all of the values of the object's own properties.
// Ignore functions
// http://underscorejs.org/#values
return Object.values(obj);
};

const mapObject = (obj, cb) => {
// Like map for arrays, but for objects. Transform the value of each property in turn.
// http://underscorejs.org/#mapObject
const objVal = {};
const objEntry = Object.entries(obj); // [[a: 1], [b: 2], [c: 3]]
for (let i = 0; i < objEntry.length; i++) {
objEntry[i][1] = cb(objEntry[i][1]);
// newObj.objEntry[i] = objEntry[i][1];
}
// magic to turn our nested arrays into objects
objEntry.forEach((a) => {
let p = objVal;
const v = a.pop();
const k = a.reduce((r, b) => {
p[r] = p[r] || {};
p = p[r];
return b;
});
p[k] = v;
});
return objVal;
};

const pairs = (obj) => {
// Convert an object into a list of [key, value] pairs.
// http://underscorejs.org/#pairs
return Object.entries(obj);
};

/* STRETCH PROBLEMS */
Expand Down
Loading