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
25 changes: 25 additions & 0 deletions src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ 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; elements.length < i; 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 newArray = [];

for (let i = 0; i < elements.length; i++) {
cb(newArray.push([i]));
}
};

const reduce = (elements, cb, startingValue) => {
Expand All @@ -28,19 +37,35 @@ 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 elementCopy = elements.slice();
let memo = startingValue || elements.Copy.shift()
each(elementCopy, (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]) === true) {
return elements[i];
}
}
};

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 res = [];
for (let i = 0; i < elements.length; i++) {
if cb(elements[i]);
}
return res;
};

/* STRETCH PROBLEM */
Expand Down
20 changes: 13 additions & 7 deletions src/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
const firstItem = (arr, cb) => {
// firstItem passes the first item of the given array to the callback function.
cb(arr[0]);// firstItem passes the first item of the given array to the callback function.
};

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

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

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

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

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

/* STRETCH PROBLEM */

const removeDuplicates = (array, cb) => {
// removeDuplicates removes all duplicate values from the given array.
const res = [];
for (let i = 0; i < array.length; i++) {
if(res.indexOf(arrray[i]) !== -1) res.push(array[i]);
}
cb(res);
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
// Do not mutate the original array.
};

/* eslint-enable */
Expand Down
21 changes: 21 additions & 0 deletions src/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,35 @@ const counter = () => {
// Example: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
let count = 0;
return () => {
count++;
return count;
};
};
const newCounter = counter();
return(newCounter());
return(newCounter());

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.
let increment = 0;
return () => {
increment++;
return increment;
};
let decrement = 0;
return () => {
decrement--;
return decrement;
};
};




const limitFunctionCallCount = (cb, n) => {
// Should return a function that invokes `cb`.
// The returned function should only allow `cb` to be invoked `n` times.
Expand Down
4 changes: 4 additions & 0 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ 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
cb(Object.mapObject(obj));
};

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