Repository navigation
Sems MVP #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
Sems MVP #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,29 +24,66 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; | |
|
|
||
| function getLength(arr, cb) { | ||
| // getLength passes the length of the array into the callback. | ||
| return cb(arr.length); | ||
| } | ||
| getLength(items, length => console.log(length)); | ||
|
|
||
|
|
||
|
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. Great use of arrow functions here. |
||
| function last(arr, cb) { | ||
| // last passes the last item of the array into the callback. | ||
| return cb(arr[arr.length - 1]); | ||
| } | ||
| last(items, lastItem => console.log(lastItem)); | ||
|
|
||
|
|
||
| function sumNums(x, y, cb) { | ||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| cb(x, y); | ||
| } | ||
| const addFunc = (a, b) => console.log(a + b); | ||
| sumNums(1, 7, addFunc); | ||
|
|
||
|
|
||
| function multiplyNums(x, y, cb) { | ||
| // multiplyNums multiplies two numbers and passes the result to the callback. | ||
| cb (x, y); | ||
| } | ||
| const multiplyFunc = (a, b) => console.log(a * b); | ||
| multiplyNums(2, 4, multiplyFunc); | ||
|
|
||
|
|
||
| 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. | ||
| for (let i = 0; i < list.length; i++) { | ||
| if(list[i] === item) { | ||
| cb(); | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| const answerForContains = str => console.log(true); | ||
| // contains('Pencil', items, answerForContains); | ||
| contains('Smart Phone', items, answerForContains); | ||
|
|
||
| /* STRETCH PROBLEM */ | ||
|
|
||
| const arrayOfDuplicates = ['Onion', 'Garlic', 'Bell Pepper', 'Garlic', 'Ginger', 'Onion', 'Garlic']; | ||
|
|
||
|
|
||
| function 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. | ||
| let newArr = []; | ||
| for (let i = 0; i < array.length; i++) { | ||
| if (!newArr.includes(array[i])) { | ||
| newArr.push(array[i]); | ||
| } | ||
| } | ||
| cb(newArr); | ||
| } | ||
| const joiningFunc = anArr => console.log(anArr.join('--')); | ||
| removeDuplicates(arrayOfDuplicates, joiningFunc) | ||
|
|
||
|
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. This was a really great solution to the stretch. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,38 @@ | ||
| // ==== Challenge 1: Write your own closure ==== | ||
| // Write a simple closure of your own creation. Keep it simple! | ||
| const kingdom = (continentName) => { | ||
| console.log(`Welcome to ${continentName}`); | ||
| const region = () => { | ||
| let continentRegion = 'The North'; | ||
| console.log(`${continentRegion} in ${continentName} will freeze you over`); | ||
| function castle() { | ||
| let continentCastle = 'Winterfell' | ||
| console.log(`Winter is coming to ${continentCastle} in ${continentRegion} of ${continentName}`); | ||
| } | ||
| castle(); | ||
| } | ||
| region(); | ||
| } | ||
| kingdom('Westeros'); | ||
|
|
||
|
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. Wonderful use of Game of Thrones. |
||
|
|
||
| // ==== Challenge 2: Create a counter function ==== | ||
| const counter = () => { | ||
| // Return a function that when invoked increments and returns a counter variable. | ||
| let count = 0; | ||
| return function() { | ||
| count++; | ||
| return count; | ||
| } | ||
| }; | ||
| let newCounter = counter(); | ||
| // Example usage: const newCounter = counter(); | ||
| // newCounter(); // 1 | ||
| // newCounter(); // 2 | ||
| console.log(newCounter()); | ||
| console.log(newCounter()); | ||
|
|
||
|
|
||
|
|
||
| /* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */ | ||
|
|
||
|
|
@@ -17,4 +41,18 @@ 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; | ||
| let counterObj = { | ||
| incrementor: function() { | ||
| return count++; | ||
| }, | ||
| decrementor: function() { | ||
| return count--; | ||
| } | ||
| } | ||
| counterObj.incrementor(); | ||
| counterObj.decrementor(); | ||
|
|
||
| }; | ||
| // console.log(counterFactory()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,32 @@ | ||
| // Take the commented ES5 syntax and convert it to ES6 arrow Syntax | ||
|
|
||
| // let myFunction = function () {}; | ||
| let myFunction = () => {}; | ||
|
|
||
| // let anotherFunction = function (param) { | ||
| // return param; | ||
| // }; | ||
| let anotherFunction = (param) => param; | ||
| console.log(anotherFunction('Hangry')); | ||
|
|
||
| // let add = function (param1, param2) { | ||
| // return param1 + param2; | ||
| // }; | ||
| // add(1,2); | ||
| let add = (param1, param2) => param1 + param2; | ||
| console.log(add(7, 1)); | ||
|
|
||
|
|
||
| // let subtract = function (param1, param2) { | ||
| // return param1 - param2; | ||
| // }; | ||
| // subtract(1,2); | ||
| let subtract = (param1, param2) => param1 - param2; | ||
| console.log(subtract(17, 9)); | ||
|
|
||
| // exampleArray = [1,2,3,4]; | ||
| exampleArray = [1,2,3,4]; | ||
| // const triple = exampleArray.map(function (num) { | ||
| // return num * 3; | ||
| // }); | ||
| // console.log(triple); | ||
| const triple = exampleArray.map((num) => num * 3); | ||
| console.log(triple); |
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.
Great conclusion. Great use of methods as well.