Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  8] [ 3]  / answers: 1 / hits: 32621  / 7 Years ago, sat, october 28, 2017, 12:00:00

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


and here is my code


function adjacentElementsProduct(inputArray) {
var arr = inputArray;
var x=0;
var y=0;
var p=0;
for(var i=0;i<arr.length;i++){
x=arr[i];
y=arr[i+1];
if(x*y>p){
p=x*y;
};
};
return p;
};

the problem is all the tests works fine but except the array with the negative product as it shown in the attached photo
can anyone help .. and thanks in advance


enter


More From » arrays

 Answers
10

You could start with a really large negative value, instead of zero.



var p = -Infinity;

[#56078] Thursday, October 26, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yulisa

Total Points: 436
Total Questions: 102
Total Answers: 123

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
yulisa questions
Sun, Jun 20, 21, 00:00, 3 Years ago
Wed, Apr 14, 21, 00:00, 3 Years ago
Fri, Aug 7, 20, 00:00, 4 Years ago
Mon, Mar 23, 20, 00:00, 4 Years ago
;