DEV Community

loading...
Cover image for Day 10 of JavaScriptmas - Adjacent Elements Product Solution

Day 10 of JavaScriptmas - Adjacent Elements Product Solution

Sekti
Keep moving forward. Learn well not learn fast.
・1 min read

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

Discussion (0)