diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..27581a424 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,22 +58,42 @@ const runners = [ // ==== 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 and populate a new array called `fullNames`. This array will contain just strings. let fullNames = []; -console.log(fullNames); + runners.forEach(function(runner){ + fullNames.push(`${runner.first_name} ${runner.last_name}`) + console.log(fullNames); + + }); // ==== Challenge 2: Use .map() ==== // The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings. let firstNamesAllCaps = []; -console.log(firstNamesAllCaps); +runners.map(function(runner){ + firstNamesAllCaps.push(`${runner.first_name} ${runner.last_name}`.toUpperCase()) + + console.log(firstNamesAllCaps); + +}); // ==== Challenge 3: Use .filter() ==== // The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects. let runnersLargeSizeShirt = []; +runners.filter(function(runner){ + runnersLargeSizeShirt.push(runner.shirt_size==="L") + +}) console.log(runnersLargeSizeShirt); + // ==== Challenge 4: Use .reduce() ==== // The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable. -let ticketPriceTotal = 0; -console.log(ticketPriceTotal); +let ticketPriceTotal = runners.reduce((acc,currentValue)=>acc+currentValue); + + + + console.log(runners.reduce(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. diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..adbc6c229 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -40,22 +40,35 @@ 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. } 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) { + let total= x*y; + return cb(total) // multiplyNums multiplies two numbers and passes the result to the callback. } function contains(item, list, cb) { + for(let i =0 ; i