diff --git a/filter1.js b/filter1.js index 602ddc0..eae56f4 100644 --- a/filter1.js +++ b/filter1.js @@ -19,6 +19,9 @@ let people = [ // (1) Write code that uses the filter function, and the below isCool function to return a list of people who are cool. -let isCool = person => person.coolnessScore > 20 +let isCool = (person) => person.coolnessScore > 20 +const coolPeople = people.filter(isCool) + +console.log(coolPeople) // Your code goes here diff --git a/filter2.js b/filter2.js index fd52f91..ada8b2f 100644 --- a/filter2.js +++ b/filter2.js @@ -3,4 +3,9 @@ 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 noStrings = misc.filter((entry) => { + return typeof entry !== "string" +}) + +console.log(noStrings) // Your code goes here diff --git a/foreach1.js b/foreach1.js index 7158aaa..75d35f0 100644 --- a/foreach1.js +++ b/foreach1.js @@ -8,4 +8,8 @@ let foods = ["pizza", "tacos", "ice cream", "sushi"]; +foods.forEach((foods) => { + console.log(`I like ${foods}`) +}) // your code here + diff --git a/foreach2.js b/foreach2.js index 7c29104..a7e327d 100644 --- a/foreach2.js +++ b/foreach2.js @@ -10,5 +10,8 @@ let foods = [ {name: "Cottage Cheese", level: "not very"} ]; +foods.forEach((food) => { + console.log(`${food.name} is ${food.level} delicious`) +}) // your code here diff --git a/map1.js b/map1.js index 9ec71f4..2a91c75 100644 --- a/map1.js +++ b/map1.js @@ -39,4 +39,20 @@ let forecast = [ // Build an array of strings using the forecast objects above with the following pattern: // "Monday will have a high of 55F and a low of 53F." +const dayOfWeek = forecast.map((day) => { + return day.day +}) +const theHigh = forecast.map((high) => { + return high.high +}) +const theLow = forecast.map((low) => { + return low.low +}) + + + +for (let i = 0; i < forecast.length; i++) { + console.log(`${dayOfWeek[i]} will have a high of ${theHigh[0]}F and a low of ${theLow[i]}F.`) +} + //Your code here diff --git a/map2.js b/map2.js index aa8ae04..cd4ad80 100644 --- a/map2.js +++ b/map2.js @@ -5,4 +5,15 @@ let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] // For each remaining element, print a string declaring it larger than 30 // e.g. "36 is larger than 30" +const squaredArray = numbers.map(nums => { + return nums**2 +}) + +const filteredArray = squaredArray.filter((nums) => { + return nums > 30 +}) + +filteredArray.forEach((nums) => { + console.log(`${nums} is larger than 30`) +}) //Your code here diff --git a/reduce.js b/reduce.js index 50a1791..4376c99 100644 --- a/reduce.js +++ b/reduce.js @@ -5,8 +5,16 @@ const numbers = [1, 56, 2, 4, 1, 99, 3, 5] // Write something that takes the above array and returns the product of all the numbers // via multiplication -// Use reduce to get the sum of all the numbers +const product = numbers.reduce((total, current) => { + return total * current +}) +console.log(product) +// Use reduce to get the sum of all the numbers +const theSum = numbers.reduce((total, current) => { + return total + current +}) +console.log(theSum) // Hungry for more: const cities = [ @@ -34,6 +42,17 @@ const cities = [ // Use reduce to get the sum of the population of all 5 cities +const populationSum = cities.reduce((total, population) => { + return population.population + total +}, 0) +console.log(populationSum) + +const mostPop = cities.reduce((total, pop) => { + return Math.max(total, pop.population) +}, 0) +console.log(mostPop) + + // Use reduce to get the city with the highest population (output should be an object) // Even hungrier: @@ -75,6 +94,22 @@ const developers = [ // Use reduce to return an object with each languages as a property // And the number of developers who know that language as that property's value + +const numLanguages = developers.reduce((objects, person) => { + for (let i = 0; i < person.languages.length; i++) { + let langSel = person.languages[i] + if(objects.hasOwnProperty(langSel)) { + objects[langSel] += 1 + } else { + objects[langSel] = 1 + } + } + return objects +}, {}) +console.log(numLanguages) + + + // Expected outcome: // { // JavaScript: 4, @@ -87,9 +122,6 @@ const developers = [ // This one is HARD // Hint 1: the desired output is an object, you might want to use that as your initial value - - - // Hint 2: Each developer's languages is stored in an array. You might need to iterate over it.