Created
January 26, 2018 08:53
-
-
Save johndavedecano/7268841fab43ef7d8e1e2e539ce6d747 to your computer and use it in GitHub Desktop.
To Find Largest Consecutive Product Of Numbers In 1D Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function solution(given) { | |
var highest = [0, 0, 0]; | |
for (var i=0; i<given.length; i++) { | |
var prev = given[i]; | |
var next = !given[i + 1] ? 0 : given[i + 1]; | |
var total = prev * next; | |
if (total > highest[2]) { | |
highest = [prev, next, prev * next]; | |
} | |
} | |
return highest; | |
} | |
var given = [1,324,525,57,87,8,6,343,77,8]; | |
var [num1, num2, product] = solution(given); // [324, 525, 170100] | |
console.log(num1, num2, num3); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment