Member-only story
JavaScript Functions For Dummies (Me): Part 2
Using the Math.max() Function
3 min readApr 3, 2018
Using the Math.max() Function
This small exercise helped me get more familiar with using Math.max()
.
The Question
// "Given an array of integers, find the pair of adjacent elements that has the largest product and return that product."// For inputArray = [3, 6, -2, -5, 7, 3], the output should be
adjacentElementsProduct(inputArray) = 21.// 7 and 3 produce the largest product.// from CodeFights
My Thought Process
// Use a for loop to run through the array.// Take the product of each adjacent element.// Push the product to a new array. // Run a separate function on the new array to output the highest integer.
1) After writing my pseudocode I opened Chrome’s JavaScript Console (Opt + Cmd + J on Mac) and originally tried this:
function adjacentElementsProduct(inputArray) {
for (var i = 0; i < inputArray.length; i++) {
let ans = inputArray[i] * inputArray[i+1];
console.log(ans);
}
}// let ans = inputArray[i] *…