Created
March 27, 2020 21:15
-
-
Save Nicknyr/5f072260cdd425e42fdacaa44cd34be2 to your computer and use it in GitHub Desktop.
CodeSign - Maximal Adjacent Difference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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