scrimba
Note at 1:01
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 1:01
AboutCommentsNotes
Note at 1:01
Expand for more info
main.js
run
preview
console
function arrayMaximalAdjacentDifference(nums) {
let maxDiff = 0;
for (let i=0; i<nums.length - 1; i++){
const currDiff = Math.abs(nums[i] - nums[i+1]);
// update maxDiff only if currDiff is bigger
currDiff > maxDiff && (maxDiff = currDiff);
}
return maxDiff;
}



/**
* Test Suite
*/
describe('arrayMaximalAdjacentDifference()', () => {
it('returns largest difference between adjacent values', () => {
// arrange
const nums = [2, 4, 1, 0];

// act
const result = arrayMaximalAdjacentDifference(nums);

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

// assert
expect(result).toBe(3);
});

it('returns largest difference between adjacent values example 2', () => {
// arrange
const nums = [2, 9, 1, 0];

// act
const result = arrayMaximalAdjacentDifference(nums);

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

// assert
expect(result).toBe(8);
});
});
Console
"Length of nums is 4"
,
7
,
8
,
1
,
"result 2: "
,
8
,
"Length of nums is 4"
,
2
,
3
,
1
,
"result 1: "
,
3
,
/index.html
LIVE