Skip to content

Instantly share code, notes, and snippets.

@Nicknyr
Created March 27, 2020 21:15
Show Gist options
  • Select an option

  • Save Nicknyr/5f072260cdd425e42fdacaa44cd34be2 to your computer and use it in GitHub Desktop.

Select an option

Save Nicknyr/5f072260cdd425e42fdacaa44cd34be2 to your computer and use it in GitHub Desktop.
CodeSign - Maximal Adjacent Difference
/*
Given an array of integers, find the maximal absolute difference between any two of its adjacent elements.
Example
For inputArray = [2, 4, 1, 0], the output should be
arrayMaximalAdjacentDifference(inputArray) = 3.
*/
function arrayMaximalAdjacentDifference(inputArray) {
let positionOfLargestDiff = 0;
let largestDiff = 0;
for(let i = 0; i < inputArray.length; i++) {
if(Math.abs(inputArray[i + 1] - inputArray[i]) > largestDiff ) {
largestDiff = Math.abs(inputArray[i + 1] - inputArray[i]);
}
else {
largestDiff = largestDiff;
}
}
return largestDiff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment