Nick Kinlen
Nick Kinlen

Reputation: 1404

Javascript: Find product of adjacent array elements and returning largest product

I'm working on sharpening my JS skills and working through some problems on CodeSignal. I'm working on the following problem:

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

So for example, given the array [2,3,5,10,2,4] I would want to do the following:

2 * 3 = 6
5 * 10 = 50
2 * 4 = 8

Then I want to return the largest product, in this case 50.

My approach is to iterate with a for loop, multiply i * i + 1, push the product into a new array, then increment the for loop by 2 so I can multiple the next two numbers in the array. When I'm done pushing into the new array I want to use Math.max and call it on the array of numbers to return the largest product.

Math.max does not work on arrays so I will use the ES6 spread operator and do something like this: Math.max(...products);

Here's what I have so far that isn't working:

function adjacentElementsProduct(inputArray) {
    var products = [];
    for(var i = 0; i <= inputArray.length; i = i + 2) {
        products.push(inputArray[i] * inputArray[i + 1]);

    };

     // Correctly logs elements of products array
     console.log(products);

     // NaN error
     console.log(Math.max(...products));

     return Math.max(...products);

}

Does this NaN error have to do with attempting to call Math.max and the spread operator before the for loop finishes and has the values pushed into the array?

Upvotes: 2

Views: 2471

Answers (3)

Asma Shajan
Asma Shajan

Reputation: 1

this is another method by using map function..

function adjacentElementsProduct(inputArray) {
    let n;
    return inputArray.map(n=>(n*(n+1)));
}
var numbers = [1,2,3,4,5,6,7];
console.log("The arrays after multiplication of adjacent numbers");
const result=adjacentElementsProduct(numbers);
console.log(result);

Upvotes: -1

Jamie Dixon
Jamie Dixon

Reputation: 54021

Since you're adding 2 to i and then looking at i and i + 1, you really only want to be looping between 0 and inputArray.length - 2.

Given your example code you can fix this by changing the for loop such that:

for(var i = 0; i <= inputArray.length - 2; i = i + 2) {
        products.push(inputArray[i] * inputArray[i + 1]);

};

Let's break that down manually so you can see it in action:

  • i = 0 and our values are [2, 3] who's product is 6
  • i = 2 and our values are [5, 10] who's product is 50
  • i = 4 and our values are [2, 4] who's product is 8.

At this point our loop ends because inputArray.length - 2 is also equal to 4 i.e. 6 - 2 === 4.

You may also want to include a check for uneven arrays so that you don't end up with the product of n and undefined. You could do that by checking the values before you find their product and ignoring any undefined containing pairs.

For some extra fun, here's a recursive version of the function:

const adjacentElementsProduct = ([a, b, ...rest], agg = []) =>  rest.length 
? adjacentElementsProduct(rest, [...agg, a * b]) 
: Math.max(...agg);

Upvotes: 1

Barmar
Barmar

Reputation: 782693

i <= inputArray.length should be i < inputArray.length. Array indexes go from 0 to array.length-1. When i == inputArray.length, you're adding two undefined values, which produces NaN.

Upvotes: 1

Related Questions