diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..07bde0cac 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -59,24 +59,41 @@ const runners = [ // 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 (fna) { + fullNames.push(`${fna.first_name} ${fna.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. +// 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); - +firstNamesAllCaps=runners.map(function(fname){ + return(fname.first_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. +// 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 = []; +//console.log(runnersLargeSizeShirt); +runnersLargeSizeShirt=runners.filter(function(lshirt){ + if(lshirt.shirt_size==="L"){ + return true; + } +}); 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; +ticketPriceTotal=runners.reduce(function(total,donat){ + return total + donat.donation; +},0); 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. +// 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 diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..18f4cb249 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -41,25 +41,56 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { // getLength passes the length of the array into the callback. + return cb(arr); +} +console.log(getLength(items,len)); +// +function len(){ + return(items.length); } function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr); +} +function enditem(){ + return(items[items.length-1]); } +//console.log(enditem(items)); + +console.log(last(items,enditem)); + function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. -} + return cb(x,y); +} +function addition(a,b){ + return a+b; +} +console.log(sumNums(4,9,addition)); function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x,y); } - +function multiply(a,b){ + return a*b; +} +console.log(multiplyNums(10,3,multiply)); 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. + return cb(item,list); } +function isfound(a,items){ + for(i=0;i {