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
56 changes: 56 additions & 0 deletions src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,26 @@ 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 NewArray = [];
for (let i = 0; i< elements.length; i++) {
newArray.push(cb(elements[i]))
return Array;
}
Copy link

Choose a reason for hiding this comment

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

watch out for your naming, it has to be consistent and you usually want to use camelCase.
so for instance NewArray should be newArray (like it is in your for-loop).
Then you want to return the newArray outside of the loop. If the return statement is inside the for-loop then the for-loop will stop looping!

};

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

};

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 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];
const flatArr = [];
for (let i = 0; i < arguments.length; i++) {
if (Array.isArray(elements[i])) {
flatArr.push(...flatten(elements[i]));
} else {
flatArr.push(elements[i]);
}
}
return flatArr;






};

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

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.
let sums = (x + y);
cb(sums)
};


const multiplyNums = (x, y, cb) => {
// multiplyNums multiplies two numbers and passes the result to the callback.
let 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(item = item)
return true
}
cb(item)
};

/* STRETCH PROBLEM */
Expand All @@ -29,6 +43,19 @@ 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 newArr = [];
for (let i = 0; i < array.length; i++) {
if ( newArr.indexOf(array[i]) === -1) {
newArr.push(array[i]);
}
}
cb(newArr);






};

/* eslint-enable */
Expand Down
62 changes: 61 additions & 1 deletion src/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,66 @@ const counter = () => {
// Example: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
};
let count = 0;
const newCounter = () => {
count++;
return count;
};
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 count = 0;
return {
increment () {
count ++;
return count
}
decrement( {
count++;
return 0 - count;
})
}





let nums = 0;
let newObject = {};
return newObject = {
increment: () => {
return nums += 1;
},
decrement: () => {
return nums -= 1;
},



Copy link

Choose a reason for hiding this comment

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

Not sure why you have two here...


};

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 = n;
return (...args) => {
if (count > 0) {
--count;
return cb(...args);
}
return null;




};

/* STRETCH PROBLEM */
Expand All @@ -27,6 +76,17 @@ const cacheFunction = (cb) => {
// 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.
function cacheFunction(cb) {
return function (number) {
number + number;
return cb(number);
}
}





};

/* eslint-enable no-unused-vars */
Expand Down
25 changes: 25 additions & 0 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@ 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 All @@ -29,12 +42,24 @@ const invert = (obj) => {
// 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 newObj = {};
Object.keys(obj).forEach(key => newObj[obj[key]] = key);
return newObj;
};

};

const defaults = (obj, defaultProps) => {
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
// Return `obj`.
// http://underscorejs.org/#defaults
Object.keys(defaultProps).forEach((key) => {
if (obj[key] === undefined) {
obj[key] = defaultProps[key];
}
});
return obj;

};

/* eslint-enable no-unused-vars */
Expand Down
Loading