Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion filter1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions filter2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions foreach1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@

let foods = ["pizza", "tacos", "ice cream", "sushi"];

foods.forEach((foods) => {
console.log(`I like ${foods}`)
})
// your code here

3 changes: 3 additions & 0 deletions foreach2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

16 changes: 16 additions & 0 deletions map1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions map2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 36 additions & 4 deletions reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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.


Expand Down