-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombine-objects.js
More file actions
34 lines (31 loc) · 1.07 KB
/
combine-objects.js
File metadata and controls
34 lines (31 loc) · 1.07 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
/*
Title:
Combine objects
Description:
Your task is to write a function that takes two or more objects and returns a new object which combines all the input objects.
All input object properties will have only numeric values. Objects are combined together so that the values of matching keys are added together.
Examples:
const objA = { a: 10, b: 20, c: 30 }
const objB = { a: 3, c: 6, d: 3 }
combine(objA, objB) // Returns { a: 13, b: 20, c: 36, d: 3 }
Notes:
The combine function should be a good citizen, so should not mutate the input objects.
Kata Link:
https://www.codewars.com/kata/combine-objects
Discuss Link:
https://www.codewars.com/kata/combine-objects/discuss
Solutions Link:
https://www.codewars.com/kata/combine-objects/solutions
*/
// Long Solution
const combine = (...objects) =>
objects.reduce((result, object) => {
for (const key in object) {
if (result[key]) {
result[key] += object[key]
} else result[key] = object[key]
}
return result
}, {})
// Function Export
module.exports = combine