Find largest adjacent product in array (JavaScript)

Find largest adjacent product in array (JavaScript)



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:



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.



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!






1 - read spread docs, or transpile to ES5 to see what it does transpiled here, 2 - because with n items in an array, you want to check n - 1 products

– Jaromanda X
Jul 4 '17 at 5:34





3 Answers
3



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.


Math#max




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.


Function#apply




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):


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.


inputArray.slice(1)


inputArray


i


inputArray






Thank you so much for the thorough explanation!

– ErnieandBert
Jul 4 '17 at 7:07






Welcome :) Don't forget to follow the links, to get more information about how spread and apply work.

– Ori Drori
Jul 4 '17 at 7:45


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.






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 8 '18 at 12:07




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);



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế