From be82dd4fc43cc5b3b67f1058a4c914149d7b9f47 Mon Sep 17 00:00:00 2001 From: jacobclapper Date: Tue, 14 Jun 2022 16:56:52 -0400 Subject: [PATCH] js-callbacks-iterators --- filter1.js | 4 ++++ filter2.js | 5 ++++- foreach1.js | 5 +++++ foreach2.js | 4 ++++ map1.js | 6 ++++++ map2.js | 14 ++++++++++++++ reduce.js | 24 +++++++++++++++++++++++- 7 files changed, 60 insertions(+), 2 deletions(-) diff --git a/filter1.js b/filter1.js index 602ddc0..354162d 100644 --- a/filter1.js +++ b/filter1.js @@ -22,3 +22,7 @@ let people = [ let isCool = person => person.coolnessScore > 20 // Your code goes here +const whoIsCool = people.filter(person => { + return person.coolnessScore > 20; +}) +console.log(whoIsCool) \ No newline at end of file diff --git a/filter2.js b/filter2.js index fd52f91..1f0b38b 100644 --- a/filter2.js +++ b/filter2.js @@ -2,5 +2,8 @@ let misc = [3, "true", {a: 1, b: 2}, 7, [1, 2, 3], ['a', 'b', 'c'], "my favorite // Use filter to filter all strings out of the above array. // Don't worry about strings in the nested array (misc[5] is considered an array, not a string) - +const allStrings = misc.filter((str) => { + return str.substring +}) // Your code goes here +console.log(allStrings) \ No newline at end of file diff --git a/foreach1.js b/foreach1.js index 7158aaa..990ac6f 100644 --- a/foreach1.js +++ b/foreach1.js @@ -9,3 +9,8 @@ let foods = ["pizza", "tacos", "ice cream", "sushi"]; // your code here + +const iLikeFoods = foods.filter((food) => { + return console.log("I like " + food) +}) +console.log(iLikeFoods) \ No newline at end of file diff --git a/foreach2.js b/foreach2.js index 7c29104..2f849f1 100644 --- a/foreach2.js +++ b/foreach2.js @@ -11,4 +11,8 @@ let foods = [ ]; // your code here +const goodFoods = foods.forEach((food) => { + return console.log(`${food.name} is ${food.level} delicious`) +}) +console.log(goodFoods) diff --git a/map1.js b/map1.js index 9ec71f4..19fd4b7 100644 --- a/map1.js +++ b/map1.js @@ -40,3 +40,9 @@ let forecast = [ // "Monday will have a high of 55F and a low of 53F." //Your code here + + +const forecastMessage = forecast.map(days => { + return `${days.day} will have a high of ${days.high} and a low of ${days.low}."` +}) +console.log(forecastMessage) \ No newline at end of file diff --git a/map2.js b/map2.js index aa8ae04..0e6deb0 100644 --- a/map2.js +++ b/map2.js @@ -6,3 +6,17 @@ let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] // e.g. "36 is larger than 30" //Your code here + +const squareEachNumber = numbers.map(num => { + return Math.pow(num, 2) +}) + +console.log(squareEachNumber) + + + +const largerThan30 = squareEachNumber.filter(overThirty => { + return overThirty > 30 +}) + +console.log(largerThan30) \ No newline at end of file diff --git a/reduce.js b/reduce.js index 50a1791..a8b30ce 100644 --- a/reduce.js +++ b/reduce.js @@ -34,8 +34,15 @@ const cities = [ // Use reduce to get the sum of the population of all 5 cities +const addPopulation = cities.reduce((world, city) => { + return city.population + world ; +}, 0) +console.log(addPopulation) // Use reduce to get the city with the highest population (output should be an object) - +// const highestPopulation = cities.reduce((world, city) => { +// return city.population + world ; +// }, 0) +// console.log(addPopulation) // Even hungrier: const developers = [ @@ -86,6 +93,21 @@ const developers = [ // This one is HARD +// reduce has optional second argument +// this will stand for the initial value +// const ageSum = instructors.reduce((sum, instructor) => { +// return sum + instructor.age ; +// }, 0) + +// console.log(ageSum) + +// const makeSum = (num1, num2) => { +// return num1 + num2.age +// } +// const ageSum = instructors.reduce(makeSum, 0) +// console.log(ageSum) + + // Hint 1: the desired output is an object, you might want to use that as your initial value