scrimba
Note at 1:23
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:23
AboutCommentsNotes
Note at 1:23
Expand for more info
main.js
run
preview
console
function arrayMaximalAdjacentDifference(nums) {
let maxDifference = Math.abs(nums[0]-nums[1]);
for (let i = 1; i < nums.length-1; i++){

//Check if existing max is less than the next test, is so swap them
if(maxDifference < Math.abs(nums[i]-nums[i+1])){ maxDifference = Math.abs(nums[i]-nums[i+1])};
//Attempt at a ternary...it works, but not sure a terenary is needed...
/* (maxDifference < Math.abs(nums[i]-nums[i+1])) ? maxDifference = Math.abs(nums[i]-nums[+1]): maxDifference;*/
}

return maxDifference;
}



/**
* 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
"result 2: "
,
8
,
"result 1: "
,
3
,
/index.html
LIVE