-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxProduct.js
More file actions
22 lines (18 loc) · 638 Bytes
/
maxProduct.js
File metadata and controls
22 lines (18 loc) · 638 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var maximumProduct = function(nums) {
nums.sort((a, b) => 0 - b - (0 - a));
const lowerProduct = nums
.slice(0, 2)
.concat(nums.slice(-1))
.reduce((acc, val) => {
return acc * val;
}, 1);
const upperProduct = nums.slice(-3).reduce((acc, val) => {
return acc * val;
}, 1);
return lowerProduct > upperProduct ? lowerProduct : upperProduct;
};
console.log(maximumProduct([29, -10, 400, 100]));
console.log(maximumProduct([1, 2, 3, 4])); // 24
console.log(maximumProduct([1, 2, 3])); // 6
console.log(maximumProduct([-1, -2, 1, 2, 3])); //6
console.log(maximumProduct([1000, 1000, 2, 1, 2, 5, 3, 1]));