Skip to content

Instantly share code, notes, and snippets.

@johndavedecano
Created January 26, 2018 08:53
Show Gist options
  • Save johndavedecano/7268841fab43ef7d8e1e2e539ce6d747 to your computer and use it in GitHub Desktop.
Save johndavedecano/7268841fab43ef7d8e1e2e539ce6d747 to your computer and use it in GitHub Desktop.
To Find Largest Consecutive Product Of Numbers In 1D Array
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