Skip to content

Instantly share code, notes, and snippets.

@brunopgalvao
Last active August 29, 2018 20:42
Show Gist options
  • Save brunopgalvao/f4ced2ad8f7f73cd5c5273a1d0d314a5 to your computer and use it in GitHub Desktop.
Save brunopgalvao/f4ced2ad8f7f73cd5c5273a1d0d314a5 to your computer and use it in GitHub Desktop.
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.
function adjacentElementsProduct(inputArray) {
let previousElement;
let largestProduct = inputArray[0]*inputArray[1];
inputArray.forEach(function(currentElement, index){
if (index === 0) {
previousElement = currentElement;
return;
}
else if (previousElement*currentElement > largestProduct) {
largestProduct = previousElement*currentElement
}
previousElement = currentElement;
});
return largestProduct;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment