-1

I'm trying to understand the following solution for finding the largest adjacent product in any given array.

Example:

 For inputArray = [3, 6, -2, -5, 7, 3], the output should be
 adjacentElementsProduct(inputArray) = 21.

 7 and 3 produce the largest product.

Possible solution in JS:

function adjacentElementsProduct(arr) {
  return Math.max(...arr.slice(1).map((x,i)=>[x*arr[i]]))
  }

I am having a hard time understanding two things:

  1. What do the three dots exactly do and how does this get passed into the function? Is there any way to write this in a more understandable way? I know that is the "spread syntax" feature in ES6, but still don't understand completely.

  2. Why do we insert "1" as argument to slice? My first though was to input "0", because we want to start at the start, then loop through everything, and see which adjacent product is the largest.

I'd appreciate any advice, links and explanations.

Thanks.

Cheers!

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
ErnieandBert
  • 91
  • 1
  • 1
  • 8
  • 1
    1 - read spread docs, or transpile to ES5 to see what it does [transpiled here](http://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=env&targets=&browsers=&builtIns=false&debug=false&experimental=true&loose=false&spec=true&code_lz=GYVwdgxgLglg9mABAQwCYCtkQKZigUQBtsBbXKAZwAUAnOVEaACmRpoEpEBvAKEURrYoIGkgCyyKAAsAdCWQAPJjJWsaMioRg4mARnZzkAByZMFAGhjsAvAD4A2goBUa-zAC679ux4BfIA), 2 - because with *n* items in an array, you want to check *n - 1* products – Jaromanda X Jul 04 '17 at 05:34

3 Answers3

1

1. What do the three dots exactly do and how does this get passed into the function? Is there any way to write this in a more understandable way? I know that is some kind of "spread" feature in ES6, but still don't understand completely.

The Math#max needs a list of numbers as parameters, and map produces an array. The spread syntax is used to convert an array to be expanded to a list of parameters.

const arr = [1, 2, 3];

console.log('max on array', Math.max(arr));

console.log('max on list of parameters', Math.max(...arr));

In this case you can use Function#apply to convert the array to a list of parameters. I find it less readable, however.

const arr = [1, 2, 3];

console.log(Math.max.apply(Math, arr));

2. Why do we insert "1" as argument to slice? My first though was to input "0", because we want to start at the start, then loop through everything, and see which adjacent product is the largest.

Lets break down the iteration order of the 2 arrays.

[3, 6, -2, -5, 7, 3] // inputArray
[6, -2, -5, 7, 3] // inputArray.slice(1)

Now on each iteration of inputArray.slice(1):

x: 6, i = 0, arr[0] = 3
x: -2, i = 1, arr[1] = 6
x: -5, i = 2, arr[2] = -2

Since the inputArray.slice(1) array starts from the 2nd element of the inputArray, the index (i) points to the 1st element of the inputArray. And the result is an array of products of 2 adjacent numbers.

Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1
var biggestProduct =  inputArray[0] *  inputArray[1];

for (i=0;  i<inputArray.length-1 ; ++i) 
{
  console.log(biggestProduct)
  if   ((inputArray[i] * inputArray[i+1] ) > biggestProduct) 
  { 
    biggestProduct = inputArray[i]  *  inputArray[i+1] 
  }
}
return biggestProduct;

Note: I've declared a variable that consists of 2 input arrays with index number then starts a for loop that indicates input array with his index number, so by that he will go throw all the index number of the array (one of them raised by one so that they won't be at the same value). and at the end of the code, you have the if statement.

moondaisy
  • 4,303
  • 6
  • 41
  • 70
Yakir Fitousi
  • 537
  • 3
  • 12
  • I've declared a variable that consists tow input arrays with index number then starts a for loop that indicates input array with his index number, so by that he will go throw all the index number of the array (one of them raised by one so that they won't be at the same value). and at the end of the code, you have the if statement. hope that was helpful. – Yakir Fitousi Oct 08 '18 at 12:07
0

You may simply do as follows;

function getNeigboringMaxProduct([x,...xs], r = -Infinity){
  var p = x * xs[0];
  return xs.length ? getNeigboringMaxProduct(xs, p > r ? p : r)
                   : r;
}

var arr = [3, 6, -2, -5, 7, 3],
    res = getNeigboringMaxProduct(arr);
console.log(res);
Redu
  • 25,060
  • 6
  • 56
  • 76