Last active
March 4, 2021 16:26
-
-
Save alexcrist/e9de987cde62778c1a2b2c9d284e1492 to your computer and use it in GitHub Desktop.
This file contains 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
function maxDiff(list) { | |
// If we get an empty list, just return 0 | |
if (list.length === 0) { | |
return 0; | |
} | |
// Initialize biggest and smallest nums to the first item in the list | |
let biggestNum = list[0]; | |
let smallestNum = list[0]; | |
// Find the biggest and smallest numbers in the list | |
for (const number of list) { | |
if (number < smallestNum) { | |
smallestNum = number; | |
} | |
if (number > biggestNum) { | |
biggestNum = number; | |
} | |
} | |
// Return their difference | |
return biggestNum - smallestNum; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment