From c8a97ae82ec620d706a271779e4c4d250a5bdb6a Mon Sep 17 00:00:00 2001 From: jamestylladams Date: Tue, 14 Jun 2022 12:03:30 -0600 Subject: [PATCH] completed lab --- filter1.js | 6 +++++- filter2.js | 6 ++++++ foreach1.js | 3 +++ foreach2.js | 3 +++ map1.js | 3 +++ map2.js | 11 +++++++++++ reduce.js | 25 +++++++++++++++++++++++-- 7 files changed, 54 insertions(+), 3 deletions(-) diff --git a/filter1.js b/filter1.js index 602ddc0..185d3b7 100644 --- a/filter1.js +++ b/filter1.js @@ -21,4 +21,8 @@ let people = [ let isCool = person => person.coolnessScore > 20 -// Your code goes here +const coolList = people.filter((person) => { + return (isCool(person)) +}) + +console.log(coolList) diff --git a/filter2.js b/filter2.js index fd52f91..b0e35b2 100644 --- a/filter2.js +++ b/filter2.js @@ -4,3 +4,9 @@ let misc = [3, "true", {a: 1, b: 2}, 7, [1, 2, 3], ['a', 'b', 'c'], "my favorite // Don't worry about strings in the nested array (misc[5] is considered an array, not a string) // Your code goes here + +let strings = misc.filter((newVal) => { + return typeof(newVal) !== "string" +}) + +console.log(strings) diff --git a/foreach1.js b/foreach1.js index 7158aaa..f2f564e 100644 --- a/foreach1.js +++ b/foreach1.js @@ -9,3 +9,6 @@ let foods = ["pizza", "tacos", "ice cream", "sushi"]; // your code here +foods.forEach((foodILike) => { + console.log(`I like ${foodILike}`) +}) diff --git a/foreach2.js b/foreach2.js index 7c29104..3a7b01f 100644 --- a/foreach2.js +++ b/foreach2.js @@ -12,3 +12,6 @@ let foods = [ // your code here +foods.forEach((food) => { + console.log(`${food.name} is ${food.level} delicious`) +}) diff --git a/map1.js b/map1.js index 9ec71f4..38841a5 100644 --- a/map1.js +++ b/map1.js @@ -40,3 +40,6 @@ let forecast = [ // "Monday will have a high of 55F and a low of 53F." //Your code here +const stringArr = forecast.map((eachFor) => { + console.log(`${eachFor.day} will have a high of ${eachFor.high} and a low of ${eachFor.low}.`) +}) \ No newline at end of file diff --git a/map2.js b/map2.js index aa8ae04..72b3c20 100644 --- a/map2.js +++ b/map2.js @@ -6,3 +6,14 @@ let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] // e.g. "36 is larger than 30" //Your code here + +const squared = numbers.map((numSquared) => { + return numSquared * numSquared +}) + +const largerThanThirty = squared.map((big) => { + if (big > 30){ + console.log(`${big} is larger than 30`) + } +}) + diff --git a/reduce.js b/reduce.js index 50a1791..901b9f5 100644 --- a/reduce.js +++ b/reduce.js @@ -1,12 +1,21 @@ const numbers = [1, 56, 2, 4, 1, 99, 3, 5] // Your code here! - +const numSum = numbers.reduce((total, current) => { + return total + current +}) +console.log(numSum) // Write something that takes the above array and returns the product of all the numbers // via multiplication - +const numProd = numbers.reduce((total, current) => { + return total * current +}) +console.log(numProd) // Use reduce to get the sum of all the numbers + + + // Hungry for more: const cities = [ @@ -32,8 +41,20 @@ const cities = [ } ] +const totalPop = cities.reduce((total, current) => { + return total + current.population +}, 0) +console.log(totalPop) + + + // Use reduce to get the sum of the population of all 5 cities + + +const biggest = cities.reduce((total, current) => { + +}, 0) // Use reduce to get the city with the highest population (output should be an object) // Even hungrier: