scrimba
Note at 0:00
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:00
AboutCommentsNotes
Note at 0:00
Expand for more info
main.js
run
preview
console
function adjacentElementsProduct(nums) {
if (!nums.length) {
throw 'input array must not be empty';
return;
}

if (nums.length === 1) {
throw 'input array must have at least two elements';
return;
}

let maxProduct = nums[0] * nums[1];
for (let i = 0; i < nums.length - 1; i++) {
if (nums[i] * nums[i + 1] > maxProduct) {
maxProduct = nums[i] * nums[i + 1];
}
}

return maxProduct;
}



/**
* 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