-2

adjacentElementsProduct is a function that takes an array as an input and we need to find and return the adjacent elements whose product is greater among all the adjacent elements.

function adjacentElementsProduct(arr) {
  return Math.max(...arr.slice(1).map((x, i) => [x * arr[i]]))
}
Sebastian Simon
  • 14,320
  • 6
  • 42
  • 61
EhtU
  • 41
  • 4
  • 2
    What exactly is unclear about this code? See [What does this symbol mean in JavaScript?](https://stackoverflow.com/q/9549780/4642212) and the documentation on MDN about [expressions and operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators) and [statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements). Read the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript) about objects and methods you don’t understand. – Sebastian Simon Aug 17 '20 at 18:08

2 Answers2

0

So:

  1. arr.slice(1) returns all the elements in the array except the first
  2. .map((x,i)=>[x*arr[i]]) will create an array pushing in it all the things that the callback return, and the the callback are returning the product of the current element with arr[i] which since on the step 1 we have taken out the first, arr[i] will be the previous (so will be something like [[first number * second], [second * third], [third * fourth], ...])
  3. ...result is the spread operator, and what it will do is pass the first element of the array result as first parameter to Math.max, the second element as second parameter...
  4. Math.max(...result) will just return the max element of the elements passed as parameter

At the end:

function adjacentElementsProduct(arr) {
  let res = arr.slice(1); // every element except first
  res = res.map((x,i)=>x*arr[i]) // array like [[first number of  of arr * third number of arr], [third * fourth], ...]
  return Math.max(...res) // max value of res
}
Berto99
  • 7,767
  • 2
  • 7
  • 22
0

The function executes as follows -

Explanation -

  • It creates a copy of original array firstly using slice(1) (read about slice()), which basically can be understood as the function creating a new array from the original one with the first element removed.

  • Then, the function maps over the newly created array. Here, the map is taking two parameters in the callback function which are

    1. x - The Value of the element and
    2. i - The Index of the element.
  • IMPORTANT STUFF HAPPENS HERE: Note that here, the map is returning an array with elements being the product of x (which is the current element under iteration of our newly created array) and arr[i] (which is the element at index i in our original array).

    So, essentially we are multiplying adjacent elements in each loop inside map(), since we have removed the first element while creating our copy array.

  • Now, this map(), as we know will return an array of elements which are nothing but the multiplications of x and arr[i] for each element x of newly created array. As already stated, its just the array of multiplication of all the adjacent elements. This array is spread into separate parameters using the spread operator syntax (...) and then passed to Math.max() which will return the maximum of all elements in the adjacent multiplication result array.


Demonstration -

Eg. Suppose our original array is -

[3,4,8,2,1]
  1. Now, the first thing that will happen is a new copy of array will be created with the first element removed. So, newArr = [4,8,2,1]

  2. Nextly, we are mapping over this array. map() callback is used with two parameters, first one being value and second one being the index currently it is on. So, we are just mapping throughout our newArr and then returning the product of x and arr[i]. So, it happens as -

    • x = 4 and i = 0 - Here, we will return x*arr[i] = 4*arr[0] = 4*3 = 12.

    • x = 8 and i = 1 - We again return x*arr[i] = 8*4 = 32.

    • x = 2 and i = 2 - We again return x*arr[i] = 2*8 = 16.

    • x = 1 and i = 3 - We again return x*arr[i] = 1*2 = 2.

  3. Do note that what the function did above is nothing but just calculated the products of all adjacent element in the original array.

  4. Now, map() returns an array of all these values that we have calculated. The array is - [12,32,16,2]. This array is split into discrete parameters and passed to Math.max() function which will just return the max of all these.

  5. We get our answer as Math.max(...[12,32,16,2]) = Math.max(12,32,16,2) = 32


const myArr = [3,4,8,2,1];

function adjacentElementsProduct(arr) {
  return Math.max(...arr.slice(1).map((x, i) => [x * arr[i]]))
  // 1. Creates new array - [4,8,2,1] using arr.slice(1)
  // 2. uses map over array to iterate over all elements of newly created array
  // 3. map returns an array which is array containing product of adjacent element of original array
  // 4. The array will be expanded into arguments using spread operator
  // 5. Math.max will return the maximum of all these arguments (which are just the multiplication of all adjacent elements of our original array)
}

console.log(adjacentElementsProduct(myArr))

Hope this clears the things.

Abhishek Bhagate
  • 5,033
  • 3
  • 10
  • 29
  • thanks for clearing.. nicely explained.. i was just not clear about why we are slicing the 0th index of the original array. also i thought that the map function was taking i(index) from the newly created array. – EhtU Aug 18 '20 at 06:01