From fc1b6ff5e0154c989814b0ecdcc81ef1470e8274 Mon Sep 17 00:00:00 2001 From: lydiathornton Date: Tue, 22 Jan 2019 14:20:56 -0800 Subject: [PATCH 1/2] started javascript-II having trouble with reduce, but still want to keep trying before i ask for help --- assignments/array-methods.js | 75 ++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index c2d782f3b..c1095adc2 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -56,28 +56,95 @@ 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(obj) { + fullName.push([obj.first_name + obj.last_name]) + return fullName; +}) +//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 = []; -console.log(allCaps); +runners.map(function(obj) { + allCaps.push(obj.first_name.toUpperCase()); +}) +//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 = []; -console.log(largeShirts); +runners.filter(function(obj) { + if (obj.shirt_size === "L") { + largeShirts.push(obj) + } + return largeShirts; +}) +//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(ticketPriceTotal, donation) { + ticketPriceTotal += donation +},0) + + + + +console.log(ticketPriceTotal); + + + + + + +//runners.forEach(function(obj) { + // ticketPriceTotal.push(obj.donation) +//}); + + console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== // Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above. // Problem 1 +let companies = [] +runners.forEach(function(obj) { + companies.push(obj.company_name) + return companies +}) +console.log(companies) // Problem 2 -// Problem 3 \ No newline at end of file +let correctEmail = [] +runners.map(function(obj) { + if (obj.email.split("@").length === 2) { + correctEmail.push(obj.email) + } + return correctEmail; +}) + +console.log(correctEmail) + +// Problem 3 tried to make a shirt size counter.. will come back to these reduce problems. +let sizes = [] +runners.forEach(function(obj) { + sizes.push(obj.shirt_size) + return sizes +}) +console.log(sizes) + +let countedSizes = sizes.reduce(function(allSizes, size) { + if (allSizes.includes(size)) { + allSizes["size"]++; + } else { + allSizes["size"]= 1; + } + return allSizes; +}) + +console.log(countedSizes); \ No newline at end of file From c5cbbae184e7b59b8718f7e86713e94b1a2ceecf Mon Sep 17 00:00:00 2001 From: lydiathornton Date: Tue, 22 Jan 2019 21:37:28 -0800 Subject: [PATCH 2/2] everything is complete --- assignments/array-methods.js | 61 ++++++++++++++++++------------------ assignments/callbacks.js | 30 ++++++++++++++++++ assignments/closure.js | 28 +++++++++++++++-- 3 files changed, 86 insertions(+), 33 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index c1095adc2..ecb55028e 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -85,27 +85,20 @@ runners.filter(function(obj) { // ==== 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(ticketPriceTotal, donation) { - ticketPriceTotal += donation +let ticketPriceTotal = +runners.reduce(function(total, runner) { + return total + runner.donation },0) +runners.reduce((total, runner) => { + return total + runner.donation +},0) -console.log(ticketPriceTotal); - - - - - - -//runners.forEach(function(obj) { - // ticketPriceTotal.push(obj.donation) -//}); +//console.log(ticketPriceTotal) -console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== // Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above. @@ -116,7 +109,7 @@ runners.forEach(function(obj) { companies.push(obj.company_name) return companies }) -console.log(companies) +//console.log(companies) // Problem 2 @@ -127,24 +120,32 @@ runners.map(function(obj) { } return correctEmail; }) +//console.log(correctEmail) + +// Problem 3 Made a shirt size counter + -console.log(correctEmail) -// Problem 3 tried to make a shirt size counter.. will come back to these reduce problems. -let sizes = [] + +let shirts = [] runners.forEach(function(obj) { - sizes.push(obj.shirt_size) - return sizes + shirts.push(obj.shirt_size) + return shirts }) -console.log(sizes) -let countedSizes = sizes.reduce(function(allSizes, size) { - if (allSizes.includes(size)) { - allSizes["size"]++; - } else { - allSizes["size"]= 1; - } - return allSizes; -}) -console.log(countedSizes); \ No newline at end of file +console.log(shirts) + + +var countedShirts = shirts.reduce(function (allShirts, shirt) { + if (shirt in allShirts) { + allShirts[shirt]++; + } + else { + allShirts[shirt] = 1; + } + return allShirts; +}, {}); + + +console.log(countedShirts) \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index d5028657e..29df255cd 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -23,26 +23,56 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { + return cb(arr.length) // getLength passes the length of the array into the callback. } +getLength(items, function(length) { + console.log(length); +}) + + function last(arr, cb) { + return cb(arr[arr.length - 1]) // last passes the last item of the array into the callback. } +last(items, function(last) { + console.log(last); +}) + function sumNums(x, y, cb) { + return cb(x + y) // sumNums adds two numbers (x, y) and passes the result to the callback. } +sumNums(6, 5, function(add) { + console.log(add) +}) + function multiplyNums(x, y, cb) { + return cb( x * y) // multiplyNums multiplies two numbers and passes the result to the callback. } +multiplyNums(5, 6, function(product) { + console.log(product); +}) + function contains(item, list, cb) { + if (list.includes(item)) { + return cb(true); + } else { + return cb(false); + } // contains checks if an item is present inside of the given array/list. // Pass true to the callback if it is, otherwise pass false. } +contains('Notebook', items, function(isThere) { + console.log(isThere) +}) + /* STRETCH PROBLEM */ function removeDuplicates(array, cb) { diff --git a/assignments/closure.js b/assignments/closure.js index b16c45ae4..3788faf9e 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -2,19 +2,41 @@ // Write a simple closure of your own creation. Keep it simple! +let decrementer = 0; +function sub() { + return decrementer -= 1 +} +console.log(sub()) + + // ==== 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 -// newCounter(); // 2 + +const newCounter = counter(); +console.log(newCounter()); // 1 +console.log(newCounter()); // 2 /* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */ // ==== Challenge 3: Create a counter function with an object that can increment and decrement ==== +let count = 0 const counterFactory = () => { + +let count = 0; +function increment() { + return count += 1 +} +function decrement() { + return count -= 1 +} // 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. }; +