So:
arr.slice(1)
returns all the elements in the array except the first
.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], ...]
)
...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...
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
}