scrimba
Note at 0:50
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Note at 0:50
AboutCommentsNotes
Note at 0:50
Expand for more info
main.js
run
preview
console
function adjacentElementsProduct(nums) {
// write code here.

// setting variables to keep track of the value
let startingValue = 1;
let maxValue = 0;
const sumOfTwo = 2
//adding the two first value to the starting value
for (let i = 0 ; i < sumOfTwo ; i++){

startingValue *= nums[i];
}

maxValue = startingValue;

for(let i = sumOfTwo; i <= nums.length - 1; i ++){
//dividing the current value by the number it was multiply
// by the value at the next index
startingValue = startingValue / nums[i - sumOfTwo] * nums[i] ;
maxValue = Math.max(maxValue , startingValue);
}

return maxValue;
}



/**
* Test Suite
*/
describe('adjacentElementsProduct()', () => {
it('returns largest product of adjacent values', () => {
// arrange
const nums = [3, 6, -2, -5, 7, 3];

// act
const result = adjacentElementsProduct(nums);

// log
console.log("result: ", result);

// assert
expect(result).toBe(21);
});
});
Console
"result: "
,
21
,
/index.html
LIVE