Skip to content

Instantly share code, notes, and snippets.

@alexcrist
Last active March 4, 2021 16:26
Show Gist options
  • Save alexcrist/e9de987cde62778c1a2b2c9d284e1492 to your computer and use it in GitHub Desktop.
Save alexcrist/e9de987cde62778c1a2b2c9d284e1492 to your computer and use it in GitHub Desktop.
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