Skip to content
Closed
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
1 change: 1 addition & 0 deletions JavaScript-I
Submodule JavaScript-I added at e2ace3
6 changes: 6 additions & 0 deletions examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = 'Mark';
console.log(name);

for (let i = 0; i <= 10; i++) {
console.log(i);
}
32 changes: 32 additions & 0 deletions src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,68 @@ 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) => {
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
// Return the new array.
const x = [];
for (let i = 0; i < elements.length; i++) {
x.push(cb(elements[i]));
}
return x;
};

const reduce = (elements, cb, startingValue) => {
// 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`.
// `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 sum = elements[0];
if (startingValue !== undefined) sum = startingValue + elements[0];
for (let i = 1; i < elements.length; i++) {
sum = cb(sum, elements[i]);
}
return sum;
};

const find = (elements, cb) => {
// 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) => {
// 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 x = [];
for (let i = 0; i < elements.length; i++) {
if (cb(elements[i])) x.push(elements[i]);
}
return x;
};

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

/* eslint-enable no-unused-vars, max-len */
Expand Down
9 changes: 9 additions & 0 deletions src/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@

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.
let result = list.indexOf(item) >= 1 ? true : false;
cb(result);
};

/* STRETCH PROBLEM */
Expand All @@ -31,6 +38,8 @@ 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 removed = array.filter((item, index, inputArray) => inputArray.indexOf(item) === index);
cb(removed);
};

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

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 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.
// Should return a function that invokes `cb`.
// The returned function should only allow `cb` to be invoked `n` times.
let callCount = 0;
return (...args) => {
if (callCount === n) return null;
callCount++;
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.
const cache = {};
return function (x) {
if (!(x in cache)) {
cache[x] = cb(x);
}
return cache[x];
};
};

/* eslint-enable no-unused-vars */
Expand Down
32 changes: 26 additions & 6 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,56 @@ 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
// Like map for arrays, but for objects. Transform the value of each property in turn.
// http://underscorejs.org/#mapObject
const mapped = {};

const myKeys = Object.keys(obj);
for (let i = 0; i < myKeys.length; i++) {
mapped[myKeys[i]] = cb(obj[myKeys[i]]);
}
return mapped;
};

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

/* STRETCH PROBLEMS */

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 inverted = {};

const keyPair = Object.keys(obj);
for (let i = 0; i < keyPair.length; i++) {
inverted[obj[keyPair[i]]] = keyPair[i];
}

return inverted;
};

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) => {
obj[key] = obj[key] || defaultProps[key];
});
return obj;
};

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