Last active
August 29, 2018 20:42
-
-
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.
This file contains 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
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