From 636eb1c2af291e01c2ac334feebc0d2028d8d43d Mon Sep 17 00:00:00 2001 From: Justine Gennaro Date: Tue, 11 Jun 2019 15:55:30 -0500 Subject: [PATCH 1/4] array challenge 1 through 4 completed --- assignments/array-methods.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f692c35c6..60b23a4b9 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -53,24 +53,41 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c {"id":49,"first_name":"Bel","last_name":"Alway","email":"balway1c@ow.ly","shirt_size":"S","company_name":"Voolia","donation":107}, {"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}]; +const newArr = ["stable","barn","table","couch","other thing"]; // ==== 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 = []; + +runners.forEach( function(element) { + return fullName.push(element.first_name + " " + 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 = []; +let allCaps = runners.map(function (element) { + return {"id": element.id, "first_name": element.first_name.toUpperCase(), "last_name": element.last_name, "email" : element.email, "shirt_size": element.shirt_size, "company_name": element.company_name, "donation": element.donation} +} ); 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 = []; +let largeShirts = runners.filter( function (element) { + if (element.shirt_size === "L") { + return true; + } +}); 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 = []; +let ticketPriceTotal = runners.reduce( function (acc, element) { + console.log(acc); + return element.donation += acc + + +},0); console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== @@ -80,4 +97,5 @@ console.log(ticketPriceTotal); // Problem 2 -// Problem 3 \ No newline at end of file +// Problem 3 + From 4f28ae1c7ad01f19405de57483fcd9c2b71301a4 Mon Sep 17 00:00:00 2001 From: Justine Gennaro Date: Tue, 11 Jun 2019 16:17:02 -0500 Subject: [PATCH 2/4] closure challenge done --- assignments/closure.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/assignments/closure.js b/assignments/closure.js index 4307524fc..ceaf4faf1 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,14 +1,41 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +let name = "justine"; + +function myFunction () { + let lastName = "Gennaro"; + return name += " " + lastName; +} +console.log(name); +console.log(myFunction()); + + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ // ==== Challenge 2: Create a counter function ==== +let count = 0; + +function counter () { + +} +const counter = function () {} const counter = () => { + let num = 1; + return count += num; // Return a function that when invoked increments and returns a counter variable. }; +console.log(count); +counter(); +console.log(count); +counter(); +console.log(count); +counter(); +counter(); +counter(); +console.log(count); // Example usage: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 From c862366ef11218792d2b4fbf1eaee89106c25e6a Mon Sep 17 00:00:00 2001 From: Justine Gennaro Date: Tue, 11 Jun 2019 16:38:49 -0500 Subject: [PATCH 3/4] callback challeng 1 and 2 done --- assignments/callbacks.js | 1 + 1 file changed, 1 insertion(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index c1c013800..180b235ca 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -26,6 +26,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { + return cb(arr.getLength) // getLength passes the length of the array into the callback. } From 6a5978dee6398508fea6945390dddb905943369a Mon Sep 17 00:00:00 2001 From: Justine Gennaro Date: Tue, 11 Jun 2019 17:33:36 -0500 Subject: [PATCH 4/4] callback challenge done --- assignments/callbacks.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 180b235ca..e3ddb2e48 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -31,26 +31,45 @@ function getLength(arr, cb) { } function last(arr, cb) { + return cb(arr.length-1) // last passes the last item of the array into the callback. } function sumNums(x, y, cb) { + return cb(x + y) // sumNums adds two numbers (x, y) and passes the result to the callback. } function multiplyNums(x, y, cb) { + return cb(x * y) // multiplyNums multiplies two numbers and passes the result to the callback. -} -function contains(item, list, cb) { - // contains checks if an item is present inside of the given array/list. +function contains(item, list, cb) { + if (list.indexOf(item) > 0) { + return cb(true) + } else { + return cb(false) + } + // check if an item is present inside of the given array/list. // Pass true to the callback if it is, otherwise pass false. } +// console.log(contains("seth", r2, uppercase); -/* STRETCH PROBLEM */ - +/* STRETCH PROBLEM - someone was trying to explain it to me*/ +// const numm = [1,2,1,3,1,2] function removeDuplicates(array, cb) { +// const myArr = array; +// const secondArr = []; +// // console.log(secondArr); +// for (let i = 0; i < myArr.length; i++) { +// console.log(`index:${i} - myArr number :${myArr[i]}`); +// console.log(`secondArr content: ${secondArr}`); +// if (secondArr.indexOf(myArr[i]) === -1) +// secondArr.push(myArr[i]) +// } +// return secondArr; // removeDuplicates removes all duplicate values from the given array. // Pass the duplicate free array to the callback function. // Do not mutate the original array. } +console.log(removeDuplicates(numm));