-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreduce.js
More file actions
45 lines (42 loc) · 1.22 KB
/
reduce.js
File metadata and controls
45 lines (42 loc) · 1.22 KB
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
// Use `.reduce` in the following methods
// Sample Input:
// [
// [ 10, 20, 30 ],
// [ -10, -4 ],
// [ 0, 10, 100, 1000 ],
// []
// ]
// Expected Output: [ 10, 20, 30, -10, -4, 0, 10, 100, 1000 ]
function flatten (matrix) {
return matrix.reduce((flat, matrixEntry) => {
return flat.concat(matrixEntry);
}, []);
}
// Sample Input:
// [
// { phone: { home: '(555) 655-4555', cell: '(333) 655-4555' } },
// { phone: { home: '(555) 455-5555', cell: '(333) 455-5555' } },
// { phone: { home: '(555) 255-5555', cell: '(333) 255-5555' } }
// ]
// Expected Output:
// {
// home: [ '(555) 655-4555', '(555) 455-5555', '(555) 255-5555' ],
// cell: [ '(333) 655-4555', '(333) 455-5555', '(333) 255-5555' ]
// }
function consolidate (numbers) {
return numbers.reduce((phoneNumbers, numberEntry) => {
if(!numberEntry){
return phoneNumbers;
}
if(!phoneNumbers.home){
phoneNumbers.home = [];
}
if(!phoneNumbers.cell){
phoneNumbers.cell = [];
}
phoneNumbers.home.push(numberEntry.phone.home);
phoneNumbers.cell.push(numberEntry.phone.cell);
return phoneNumbers;
}, {});
}
module.exports = { flatten, consolidate }