- Notifications
You must be signed in to change notification settings - Fork106
Initial commit#103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
rusticlion wants to merge2 commits intobloominstituteoftechnology:masterChoose a base branch fromrusticlion:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Initial commit#103
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletionsarrays.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Complete the following functions. | ||
| // These functions only need to work with arrays. | ||
| // Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc. | ||
| // You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating | ||
| // You can use the functions that you have already written to help solve the other problems | ||
| const each = (elements, cb) => { | ||
| // Iterates over a list of elements, yielding each in turn to the `cb` function. | ||
| // 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 newArray = []; | ||
| for (let i = 0; i < elements.length; i++) { | ||
| newArray.push(cb(elements[i])); | ||
| } | ||
| return newArray; | ||
| }; | ||
| 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 memo = (startingValue === undefined) ? elements[0] : startingValue; | ||
| let i = (startingValue === undefined) ? 1 : 0; | ||
| for (; i < elements.length; i++) { | ||
| memo = cb(memo, elements[i]); | ||
| } | ||
| return memo; | ||
| }; | ||
| 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 newArr = []; | ||
| for (let i = 0; i < elements.length; i++) { | ||
| if (cb(elements[i])) newArr.push(elements[i]); | ||
| } | ||
| return newArr; | ||
| }; | ||
| /* 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]; | ||
| }; | ||
| /* eslint-enable no-unused-vars, max-len */ | ||
| module.exports = { | ||
| each, | ||
| map, | ||
| reduce, | ||
| find, | ||
| filter, | ||
| flatten, | ||
| }; |
65 changes: 65 additions & 0 deletionscallbacks.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* eslint-disable */ | ||
| 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. | ||
| const arrayLength = arr.length; | ||
| cb(arr[arrayLength - 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 sum = x * y; | ||
| cb(sum); | ||
| }; | ||
| 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 === list[i]) return cb(true); | ||
| } | ||
| return cb(false); | ||
| }; | ||
| /* STRETCH PROBLEM */ | ||
| 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++){ | ||
| contains(array[i], newArr, (isDuplicate) => {if(!isDuplicate) { | ||
| newArr.push(array[i]); | ||
| } | ||
| }) | ||
| } | ||
| cb(newArr); | ||
| }; | ||
| /* eslint-enable */ | ||
| module.exports = { | ||
| firstItem, | ||
| getLength, | ||
| last, | ||
| sumNums, | ||
| multiplyNums, | ||
| contains, | ||
| removeDuplicates, | ||
| }; |
58 changes: 58 additions & 0 deletionsclosure.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // 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 | ||
| let tracker = 0; | ||
| return () => { return ++tracker; }; | ||
| }; | ||
| 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 tracker = 0; | ||
| return { | ||
| increment: () => { | ||
| return ++tracker; | ||
| }, | ||
| decrement: () => { | ||
| return --tracker; | ||
| }, | ||
| }; | ||
| }; | ||
| const limitFunctionCallCount = (cb, n) => { | ||
| // Should return a function that invokes `cb`. | ||
| // The returned function should only allow `cb` to be invoked `n` times. | ||
| let i = 0; | ||
| return (...args) => { | ||
| if (i < n) { | ||
| i++; | ||
| return cb(...args); | ||
| } | ||
| return null; | ||
| }; | ||
| }; | ||
| /* 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. | ||
| }; | ||
| /* eslint-enable no-unused-vars */ | ||
| module.exports = { | ||
| counter, | ||
| counterFactory, | ||
| cacheFunction, | ||
| limitFunctionCallCount, | ||
| }; |
82 changes: 82 additions & 0 deletionsobjects.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Complete the following underscore functions. | ||
| // Reference http://underscorejs.org/ for examples. | ||
| 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 | ||
| const keyArray = []; | ||
| Object.keys(obj).forEach((key) => { | ||
| keyArray.push(key); | ||
| }); | ||
| return keyArray; | ||
| }; | ||
| 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 | ||
| keys(obj).forEach((key) => { | ||
| obj[key] = cb(obj[key]); | ||
| }); | ||
| return obj; | ||
| }; | ||
| const pairs = (obj) => { | ||
| // Convert an object into a list of [key, value] pairs. | ||
| // http://underscorejs.org/#pairs | ||
| const list = []; | ||
| keys(obj).forEach((key) => { | ||
| const pair = []; | ||
| pair.push(key); | ||
| pair.push(obj[key]); | ||
| list.push(pair); | ||
| }); | ||
| return list; | ||
| }; | ||
| /* 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 invertedObject = {}; | ||
| keys(obj).forEach((key) => { | ||
| const keyHolder = key; | ||
| const valueHolder = obj[key]; | ||
| invertedObject[valueHolder] = keyHolder; | ||
| }); | ||
| return invertedObject; | ||
| }; | ||
| const defaults = (obj, defaultProps) => { | ||
| // Fill in undefined properties that match properties on the `defaultProps` parameter object. | ||
| // Return `obj`. | ||
| // http://underscorejs.org/#defaults | ||
| keys(defaultProps).forEach((key) => { | ||
| if (obj[key] === undefined) { | ||
| keys(defaultProps).forEach((defaultKey) => { | ||
| if (defaultKey === key) obj[key] = defaultProps[defaultKey]; | ||
| }); | ||
| } | ||
| }); | ||
| return obj; | ||
| }; | ||
| /* eslint-enable no-unused-vars */ | ||
| module.exports = { | ||
| keys, | ||
| values, | ||
| mapObject, | ||
| pairs, | ||
| invert, | ||
| defaults, | ||
| }; |
1 change: 1 addition & 0 deletionsscript.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| console.log("Hello world!"); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.