forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Better understanding of first assignment #12
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
deytonk
wants to merge
4
commits into
gh-pages
Choose a base branch
from
class10-higherOrderFunctions
base: gh-pages
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,134 @@ | ||
| 'use strict'; | ||
|
|
||
| // Create a forEach() function that takes an array of items and a function that runs the function arr.length number of times. | ||
| // let because for each does not return a new array | ||
| let forEachArr = [1, 2, 3, 4, 5, 6]; | ||
|
|
||
| const whatForEachDoes = (arr, callbackFunc) => { | ||
| for (i = 0; i < forEachArr.length; i++) { | ||
| forEachArr.push(callbackFunc(i)); | ||
| } | ||
| }; | ||
|
|
||
| whatForEachDoes(forEachArr, (num) => num + 1); | ||
|
|
||
| // Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. | ||
|
|
||
| const mapArr = [1, 2, 3, 4, 5, 6]; | ||
|
|
||
| const whatMapDoes = (arr, callbackFunc) => { | ||
| let newArr = []; | ||
| mapArr.forEach((i) => { | ||
| newArr.push(callbackFunc(i)); | ||
| }); | ||
| return newArr; | ||
| }; | ||
|
|
||
| whatMapDoes(mapArr, (num) => num + 1); | ||
|
|
||
| // Create a filter() function that takes an array of items and a function that returns an array with only the items that return true in the function. | ||
|
|
||
| const filterArr = [1, 2, 3, 4, 5, 6]; | ||
|
|
||
| const whatFilterDoes = (arr, callbackFunc) => { | ||
| let newArr = []; | ||
| filterArr.forEach((i) => { | ||
| newArr.push(callbackFunc(i)); | ||
| }); | ||
| }; | ||
|
|
||
| whatFilterDoes(filterArr, (num) => num < 3); | ||
|
|
||
| // For your project, we are going to go back into time and re-implement all of these higher-order functions from scratch. Place your code in the /06week/higherOrder.js file in your workbook. | ||
| // | ||
| // 1. Create a some() function that takes an array of items and a function that returns true or false if any of the items return true in the function. | ||
|
|
||
| const mySome = (arr, callBack) => { | ||
| let somethingPasses = false; | ||
| const breakException = {}; | ||
| arr.forEach((item) => { | ||
| if (callBack(item)) { | ||
| somethingPasses = true; | ||
| throw breakException; | ||
| } | ||
| }); | ||
|
|
||
| return somethingPasses; | ||
| }; | ||
|
|
||
| const someArr = [5, 7, 10, 400, 10000, 5]; | ||
| const test = mySome(someArr, (item) => {return item > 50}); | ||
|
|
||
| console.log(test); | ||
|
|
||
| // 2. Create an every() function that takes an array of items and a function that returns true or false if all of the items return true in the function. | ||
|
|
||
| const myEvery = (arr, callBack) => { | ||
| let somethingPasses = true; | ||
| const breakException = {}; | ||
| arr.forEach((item) => { | ||
| if (callBack(item)) { | ||
| somethingPasses = false; | ||
| throw breakException; | ||
| } | ||
| }); | ||
|
|
||
| return somethingPasses; | ||
| }; | ||
|
|
||
| const everyArr = [5, 7, 10, 400, 10000, 5]; | ||
| const test = myEvery(everyArr, (item) => {return item > 50}); | ||
|
|
||
| console.log(test); | ||
|
|
||
|
|
||
|
|
||
| // 1. Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. | ||
|
|
||
| const test = [1, 2, 3, 4, 5, 6]; | ||
|
|
||
| const whatMapDoes = (arr, callbackFunc) => { | ||
| const newArr = []; | ||
| arr.forEach((i) => { | ||
| newArr.push(callbackFunc(i)); | ||
| }); | ||
| return newArr; | ||
| }; | ||
|
|
||
| whatMapDoes(test, (num) => num + 1); | ||
|
|
||
| // The map() method for reference: | ||
| const test2 = test.map((num, index) => { | ||
| return num + 1; | ||
| }); | ||
|
|
||
| console.log(test2); | ||
|
|
||
| console.log(test.map((num, index) => num + 1)); | ||
|
|
||
| // 2. Create an object and store it to a variable called userObject. It must have at least 8 key/value pairs. | ||
|
|
||
| const userObject = { | ||
| name: 'Deyton', | ||
| middleName: 'A', | ||
| lastName: 'Koch', | ||
| age: 27, | ||
| eyeColor: 'Blue', | ||
| hairColor: 'Brown', | ||
| greeting: 'Hello!', | ||
| favoriteMovie: 'Wizard of Oz', | ||
| }; | ||
|
|
||
| // 3. Create an array from all of the keys in userObject. Store the array in a variable called userKeyArray. | ||
|
|
||
| const userKeyArray = Object.keys(userObject); | ||
| console.log(userKeyArray); | ||
|
|
||
| // Create an array of all of the values in userObject using userKeyArray and your map() function, store it in a variable called userValueArray. | ||
|
|
||
| // const userValueArray = Object.values(userObject); | ||
| // console.log(userValueArray); | ||
|
|
||
| const userValueArray = []; | ||
|
|
||
| whatMapDoes(userValleyArray, (userKeyArray) => userKeyArray.values(userObject)); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the exact same as map, you need to add in the filter aspect. You are also missing a return