-
Notifications
You must be signed in to change notification settings - Fork 0
Larry simiyu #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
base: master
Are you sure you want to change the base?
Larry simiyu #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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "workbench.colorCustomizations": { | ||
| "activityBar.background": "#352D0A", | ||
| "titleBar.activeBackground": "#4A400F", | ||
| "titleBar.activeForeground": "#FCFAF2" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -56,21 +56,48 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c | |||||||||
| // ==== Challenge 1: Use .forEach() ==== | ||||||||||
| // The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName. | ||||||||||
| let fullName = []; | ||||||||||
| console.log(fullName); | ||||||||||
|
|
||||||||||
| runners.forEach(function(element){ | ||||||||||
| fullName.push(element.first_name); | ||||||||||
| fullName.push(element.last_name); | ||||||||||
|
|
||||||||||
| }); | ||||||||||
|
|
||||||||||
| console.log(fullName); | ||||||||||
|
|
||||||||||
|
|
||||||||||
| // ==== Challenge 2: Use .map() ==== | ||||||||||
| // The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result | ||||||||||
| let allCaps = []; | ||||||||||
|
|
||||||||||
| runners.map(function(currentValue){ | ||||||||||
| return allCaps.push(currentValue.first_name.toUpperCase()); | ||||||||||
|
|
||||||||||
| }); | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
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 work here!
Suggested change
|
||||||||||
| console.log(allCaps); | ||||||||||
|
|
||||||||||
| // ==== Challenge 3: Use .filter() ==== | ||||||||||
| // The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result | ||||||||||
| let largeShirts = []; | ||||||||||
|
|
||||||||||
| runners.filter((currentValue) => { | ||||||||||
| return largeShirts.push(currentValue.shirt_size === "L"); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
|
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 job here! |
||||||||||
| console.log(largeShirts); | ||||||||||
|
|
||||||||||
| // ==== Challenge 4: Use .reduce() ==== | ||||||||||
| // The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result | ||||||||||
| let ticketPriceTotal = []; | ||||||||||
|
|
||||||||||
| runners.reduce(function(accumilator, currentValue){ | ||||||||||
|
|
||||||||||
| return ticketPriceTotal.push(accumilator + currentValue.donation); | ||||||||||
| }, 0); // starting point - starts from 0 | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
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. FYI the accumulator is a 'foo bar' thing (i.e. you can name it whatever you wish). |
||||||||||
| console.log(ticketPriceTotal); | ||||||||||
|
|
||||||||||
| // ==== Challenge 5: Be Creative ==== | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; | ||
|
|
||
|
|
||
| /* | ||
|
|
||
| //Given this problem: | ||
|
|
@@ -24,19 +25,46 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; | |
|
|
||
| function getLength(arr, cb) { | ||
| // getLength passes the length of the array into the callback. | ||
| return cb(arr); | ||
|
LarrySimiyu marked this conversation as resolved.
|
||
| } | ||
| getLength(items,arrLength); | ||
| function arrLength(arr) { | ||
| console.log(arr.length); | ||
| } | ||
|
|
||
|
|
||
| function last(arr, cb) { | ||
| // last passes the last item of the array into the callback. | ||
|
|
||
|
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. So this function would be your callback function. You seem to be adding your cb into your test functions. Let's talk about this. |
||
| cb(arr); | ||
| } | ||
| last(items, lastFunc); | ||
| function lastFunc(arr) { | ||
| console.log(arr[arr.length - 1]); | ||
| } | ||
|
|
||
|
|
||
| function sumNums(x, y, cb) { | ||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| return cb(x,y); | ||
|
|
||
| } | ||
| sumNums(2,7,total); | ||
| function total(x,y) { | ||
| console.log(x+y); | ||
|
|
||
| } | ||
|
|
||
| function multiplyNums(x, y, cb) { | ||
| // multiplyNums multiplies two numbers and passes the result to the callback. | ||
| return cb(x,y); | ||
| } | ||
| multiplyNums(3,5, mult); | ||
| function mult(x,y) { | ||
| console.log(x*y); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| function contains(item, list, cb) { | ||
| // contains checks if an item is present inside of the given array/list. | ||
|
|
@@ -50,3 +78,24 @@ function removeDuplicates(array, cb) { | |
| // Pass the duplicate free array to the callback function. | ||
| // Do not mutate the original array. | ||
| } | ||
|
|
||
|
|
||
| // function higherOrderFunction(param1,param2,callback) { | ||
| // // console.log(`${greeting}, this is my random number: ${random}`); | ||
| // callback(param1,param2); | ||
| // } | ||
|
|
||
| // higherOrderFunction(3,1,product); | ||
|
|
||
| // // Callback function definition | ||
| // function add(a,b) { | ||
| // console.log(a+b); | ||
| // } | ||
|
|
||
| // function subtract(a,b) { | ||
| // console.log(a-b); | ||
| // } | ||
|
|
||
| // function product(a,b) { | ||
| // console.log(a*b); | ||
|
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. I want to go over callbacks with you. |
||
| // } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,26 @@ | ||
| // ==== Challenge 1: Write your own closure ==== | ||
| // Write a simple closure of your own creation. Keep it simple! | ||
|
|
||
| function anyFunc(name) { | ||
| console.log(name); | ||
| let newName = name; | ||
|
|
||
| function printName() { | ||
| debugger; | ||
| console.log(newName); | ||
| } | ||
| } | ||
|
|
||
| anyFunc("larry"); | ||
|
|
||
|
|
||
| // ==== 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() { | ||
| return count += 1; | ||
| } | ||
| }; | ||
| // Example usage: const newCounter = counter(); | ||
| // newCounter(); // 1 | ||
|
|
@@ -17,4 +33,15 @@ 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; | ||
|
|
||
| return { | ||
| increment() { | ||
| return count += 1; | ||
| }, | ||
| decrement() { | ||
| return count -= 1; | ||
| } | ||
|
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. |
||
| } | ||
| }; | ||
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.
Excellent job, Larry. Here is what that same function would look like using es6 arrow syntax, btw (although both ways are totally valid!!):