From d0d23ce034b2e7d83febb4f87a61882b336b57f8 Mon Sep 17 00:00:00 2001 From: Phil B Date: Wed, 5 Sep 2018 17:43:05 +0100 Subject: [PATCH 1/2] Chris and Phil's work from Wednesday --- src/index.js | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 3e68d32..7abe60c 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ function removeMiddle( words ){ // words is an array which contains an odd number of strings // return a new array containing only the middle word // the words array should no longer contain the middle word - + return words.splice(Math.floor(words.length/2),1); // hint: use splice } @@ -10,23 +10,26 @@ function get2ndAnd3rd( myArray ){ // myArray is an array of numbers // return an array containing the 2nd and 3rd items from myArray // myArray should remain unchanged - +return myArray.slice(1,3); // hint: use slice } function stringify( myArray ){ + return myArray.map(item => item.toString()); // myArray is an array of numbers // return a new array which has all items converted to strings // myArray should remain unchanged } function types(input){ + return input.map(item => item===null?"null":typeof item); // input is an array of items of all types // return an array which contains the // type of each of the items in input array } function wordLengths( words ){ + return words.map(item => item.length); // words is an array of strings // return a new array that contains the number of letters in each word // for example @@ -37,6 +40,7 @@ function wordLengths( words ){ } function cities( capitals, formatter ){ + return capitals.map(item => formatter(item)); // capitals is an array of objects that have a city and country property // for example // { @@ -51,47 +55,57 @@ function cities( capitals, formatter ){ } function largerThanTen( numbers ){ + return numbers.filter(item => item > 10); + // numbers is an array of numbers // return a new array that contains only numbers // from the input array which are greater than 10 } function even( numbers ){ + return numbers.filter(item => item % 2 === 0); // numbers is an array of numbers // return a new array that contains only even numbers from the input array } function findTheNeedle( words ){ + return words.indexOf("needle"); // words is an array of words // return the index of the word 'needle' } function findLargest( numbers ){ + return Math.max(...numbers); // numbers is an array of numbers // return the largest number from that array } function addAllnumbers( numbers ) { + return numbers.reduce(function(acc,item) {return acc + item} ,0); // numbers is an array of numbers // return the sum of all the numbers in the array } function sortingStrings(strings){ + return strings.sort(); // strings is an array of strings // sort them in alphabetical order and return the sorted array } function sortingNumbers(numbers){ +return numbers.sort((a,b) => a - b); // numbers is an array of numbers // sort them in ascending order and return the sorted array } function sortingNumbersDescending(numbers){ + return numbers.sort((a,b) => b - a); // numbers is an array of numbers // sort them in descending order and return the sorted array } function sortingCars(cars){ + return cars.sort((a,b) => a.year - b.year); // a car object has a make, model, year. For example // const car = { // make: 'Ford', @@ -104,6 +118,8 @@ function sortingCars(cars){ } function deleteColour( car ){ + delete car.colour; + return car; // car is an object with properties make, model and colour. For example // { // make: 'Ford', @@ -115,6 +131,20 @@ function deleteColour( car ){ }; function paintShop( cars, colour ){ + //var newCars = Object.assign({}, cars); + // let newCars = cars.map(item => Object.assign({}, item)) + + return cars.map(function(item) { + if (item.make==="Ford") { + return Object.assign({},item, {colour}); + } + return item; + + }) + + + console.log("END"); + // cars is an array of objects that have // their properties are `make`, `model` and `colour` @@ -133,6 +163,12 @@ function paintShop( cars, colour ){ } function secondLargest( numbers ){ + + const numbersClone = numbers.slice(0); + const sorted = numbersClone.sort((a,b) => b - a); + const secondLargest = sorted[1]; + return numbers.indexOf(secondLargest); + // numbers is an array of numbers // return the index of the second // largest number in the array From dfb88d4c47a74ca4dcf4fe99aa8c1758ba721b34 Mon Sep 17 00:00:00 2001 From: Phil B Date: Thu, 6 Sep 2018 12:02:24 +0100 Subject: [PATCH 2/2] array methods task --- src/index.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 7abe60c..d75f8d6 100644 --- a/src/index.js +++ b/src/index.js @@ -192,6 +192,13 @@ function addSales( city, sales ){ // property and save the sales figure as its value. // return the updated globalSales object + if (globalSales.hasOwnProperty(city)) { + globalSales[city] += sales; + } else { + globalSales[city] = sales; + } + + return globalSales; } function totalSales( sales ){ @@ -200,9 +207,15 @@ function totalSales( sales ){ // and return the total. // Hint: look up how to get the values of an object + const salesArray = Object.values(sales); + console.log(salesArray); + return salesArray.reduce(function(acc,item){return acc + item}, 0); } function highestSum( input ){ + + const summedArrays = input.map(item => item.reduce(function(acc,i){return acc + i},0)); + return Math.max(...summedArrays); // input is an array of arrays of numbers. // For example // [ @@ -229,7 +242,7 @@ function walletSum( wallet ){ // // calculate the sum of money in the wallet and return // the total. - + return (wallet[5] * 5) + (wallet[10] * 10) + (wallet[20] * 20); // the denominations used in this exercise are 5, 10 and 20 } @@ -238,16 +251,78 @@ function walletMerge( wallet1, wallet2 ){ // both wallets passed in. // the denominations used in this exercise are 5, 10 and 20 + + + const fives = wallet1[5] + wallet2[5]; + const tens = wallet1[10] + wallet2[10]; + const twenties = wallet1[20] + wallet2[20]; + + return {5: fives, 10: tens, 20: twenties}; + + } function arrayOfWallets( wallets ){ // wallets is an array of wallets + +let five = 0; +let ten = 0; +let twenty = 0; + +wallets.forEach(function (wallet) { + five += wallet[5]; + ten += wallet[10]; + twenty += wallet[20] +}); + +return {5: five, 10: ten, 20: twenty}; + + + // Return a new wallet object containing the notes from all wallets // the denominations used in this exercise are 5, 10 and 20 } + + function crazyMoney( wallets ){ + let valueOfWallet = 0; + let notes = []; + + wallets.map(function (wallet) { + notes=Object.keys(wallet); + valueOfWallet += notes.reduce((acc,note) => acc + note*wallet[note], 0); + }); + + return valueOfWallet; + +} + + + + + // let valueOfWallet = 0; + + // wallets.map(function (wallet) { + // for (var note in wallet) { + // valueOfWallet += note * wallet[note] + // } + // }); + + // return valueOfWallet; + + + + + // for (var note in oneWallet) { + // console.log(note * oneWallet[note]); + // valueOfWallet += note * oneWallet[note] + + // }; + // console.log(valueOfWallet); + + // In previous exercises the notes denominations were // limited to 5, 10 and 20. We now have wallets that // can have notes of any denomination. Implement a function @@ -257,7 +332,7 @@ function crazyMoney( wallets ){ // Hint: look up how to get keys of objects. You may also need // to use square bracket notation to look up values which // correspond to those keys -} + /* * STRETCH GOALS