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
37 changes: 37 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 newArr = [];
for (let i = 0; i < elements.length; i++) {
newArr.push(cb(elements[i]));
}
return newArr;
};

const reduce = (elements, cb, startingValue) => {
Expand All @@ -28,19 +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.
let startingIndex = 0;
let temp = startingValue;
if (startingValue === undefined) {
temp = elements[0];
startingIndex = 1;
}
for (let i = startingIndex; i < elements.length; i++) {
temp = cb(temp, elements[i]);
}
return temp;
};

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];
}
}
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 arr = [];
let pass = false;
for (let i = 0; i < elements.length; i++) {
if (cb(elements[i]) === true) {
arr.push(elements[i]);
pass = true;
}
}
if (pass === false) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this if statement. You can just return the array. if it's empty... it's empty.

return [];
} else if (pass === true) {
return arr;
}
};

/* STRETCH PROBLEM */
Expand Down
13 changes: 13 additions & 0 deletions src/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
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(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.
const sum = x + y;
cb(sum);
};

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

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

/* STRETCH PROBLEM */
Expand Down
24 changes: 23 additions & 1 deletion src/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,45 @@ const counter = () => {
// Example: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
let count = 1;
function increment() {
return count++;
}
return increment;
};

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

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

/* STRETCH PROBLEM */

const cacheFunction = (cb) => {
// Should return a funciton that invokes `cb`.
// Should return a function 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
Expand Down
10 changes: 10 additions & 0 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@ 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 newObj = {};
const key = Object.keys(obj);
for (let i = 0; i < key.length; i++) {
const newValue = cb(obj[key[i]]);
newObj[key[i]] = newValue;
}
return newObj;
};

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
12 changes: 2 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1359,14 +1359,10 @@ extglob@^2.0.4:
snapdragon "^0.8.1"
to-regex "^3.0.1"

[email protected]:
[email protected], extsprintf@^1.2.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05"

extsprintf@^1.2.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f"

fast-deep-equal@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.1.0.tgz#c053477817c86b51daa853c81e059b733d023614"
Expand Down Expand Up @@ -2440,18 +2436,14 @@ minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4:
dependencies:
brace-expansion "^1.1.7"

[email protected]:
[email protected], minimist@~0.0.1:
version "0.0.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"

minimist@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"

minimist@~0.0.1:
version "0.0.10"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"

mixin-deep@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/mixin-deep/-/mixin-deep-1.3.1.tgz#a49e7268dce1a0d9698e45326c5626df3543d0fe"
Expand Down