forked from bloominstituteoftechnology/JavaScript-II
-
Notifications
You must be signed in to change notification settings - Fork 0
Javascript-II #1
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
Merged
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| // Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems. | ||
| // Create a higher order function and invoke the callback function to test your work. | ||
| // You have been provided an example of a problem and a solution to see how this works with our items array. | ||
| // Study both the problem and the solution to figure out the rest of the problems. | ||
|
|
||
| const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; | ||
|
|
||
|
|
@@ -24,27 +26,51 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; | |
|
|
||
| */ | ||
|
|
||
|
|
||
| function getLength(arr, cb) { | ||
| // getLength passes the length of the array into the callback. | ||
| function getLength(arr, cb) { | ||
| cb(arr.length); | ||
| } | ||
| getLength(items, (lengthOfArr) => { | ||
| console.log(lengthOfArr); | ||
| }); | ||
|
|
||
| function last(arr, cb) { | ||
| // last passes the last item of the array into the callback. | ||
| function last(arr, cb) { | ||
| cb(arr[3]) | ||
| } | ||
| last(items, (lastItem) => { | ||
| console.log(lastItem); | ||
| }); | ||
|
|
||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| function sumNums(x, y, cb) { | ||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| cb(x + y); | ||
| } | ||
|
|
||
| function multiplyNums(x, y, cb) { | ||
| sumNums(2, 3, (add) => { | ||
| console.log(add); | ||
| }); | ||
|
|
||
| // multiplyNums multiplies two numbers and passes the result to the callback. | ||
| function multiplyNums(x, y, cb) { | ||
| cb(x * y); | ||
| } | ||
| multiplyNums(3, 3, (multiply) => { | ||
| console.log(multiply); | ||
| }) | ||
|
|
||
| // contains checks if an item is present inside of the given array/list. | ||
| //Pass true to the callback if it is, otherwise pass false. | ||
| function 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. | ||
| } | ||
| const checks = () => { | ||
| for (let i = 0; i < list.length; i++) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how can you convert this into es6 instead of using for loops |
||
| if (list[i] === item) { | ||
| return true; | ||
| } | ||
| } return false; | ||
| }; | ||
| cb(checks()); | ||
| }; | ||
|
|
||
|
|
||
| /* STRETCH PROBLEM */ | ||
|
|
||
|
|
||
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 |
|---|---|---|
| @@ -1,14 +1,36 @@ | ||
| // ==== Challenge 1: Write your own closure ==== | ||
| // Write a simple closure of your own creation. Keep it simple! | ||
|
|
||
| function closureOuter() { | ||
| var x = "Hi"; | ||
| function closureInner() { | ||
| console.log(x); | ||
| } | ||
| console.log('test', x); | ||
| return closureInner; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good job on the closure |
||
| closureOuter(); | ||
|
|
||
| /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ | ||
|
|
||
|
|
||
| // ==== Challenge 2: Create a counter function ==== | ||
| const counter = () => { | ||
| // Return a function that when invoked increments and returns a counter variable. | ||
| let count = 0 | ||
| const myFunction = () => { | ||
| count = ++count | ||
| return count | ||
| } | ||
| return myFunction | ||
| }; | ||
|
|
||
| const increment = counter() | ||
| const c1 = increment() | ||
| const c2 = increment() | ||
| const c3 = increment() | ||
|
|
||
| console.log('increment:', c1, c2, c3); | ||
| // Return a function that when invoked increments and returns a counter variable. | ||
| // Example usage: const newCounter = counter(); | ||
| // newCounter(); // 1 | ||
| // newCounter(); // 2 | ||
|
|
||
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.
use const until you can't keep that in mind