forked from WDI-SEA/js-callbacks-iterators
/
reduce.js
97 lines (78 loc) · 1.84 KB
/
reduce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const numbers = [1, 56, 2, 4, 1, 99, 3, 5]
// Your code here!
// 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
// Hungry for more:
const cities = [
{
name: 'Chicago',
population: 2746388
},
{
name: 'New York City',
population: 8804190
},
{
name: 'Boston',
population: 689326
},
{
name: 'Atlanta',
population: 497642
},
{
name: 'Los Angeles',
population: 3898747
}
]
// Use reduce to get the sum of the population of all 5 cities
// Use reduce to get the city with the highest population (output should be an object)
// Even hungrier:
const developers = [
{
name: 'Mike',
languages: [
'JavaScript',
'Python'
]
},
{
name: 'Maria',
languages: [
'JavaScript',
'Ruby'
]
},
{
name: 'Becky',
languages: [
'JavaScript',
'Python',
'Rust',
'C'
]
},
{
name: 'John',
languages: [
'JavaScript',
'Python',
'Ruby'
]
}
]
// 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
// Expected outcome:
// {
// JavaScript: 4,
// Python: 3,
// Ruby: 2,
// Rust: 1,
// C: 1
// }
// 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.
// Instead of hardcoding the languages in your initial object, can you think of a way to do this programmatically?