DEV Community

Cover image for Day 10 of JavaScriptmas - Adjacent Elements Product Solution
Sekti Wicaksono
Sekti Wicaksono

Posted on

Day 10 of JavaScriptmas - Adjacent Elements Product Solution

Day 10 is finding the biggest adjacent element of product in an array.

For example an array [3,6,-2,-5,7,3] the output should be 21 because 7*3=21 is the biggest adjcent product in an array.

This is JavaScript solution

function adjacentElementsProduct(nums) {

    let biggest = 0;

    for (let i = 0; i < nums.length; i++) {
        if(biggest < nums[i]*nums[i+1]) {
            biggest = nums[i]*nums[i+1];
        }        
    }

    return biggest;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)