Skip to content

Instantly share code, notes, and snippets.

@adyngom
Created June 13, 2019 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adyngom/f45e00018fdda33ef15dc31a5324413a to your computer and use it in GitHub Desktop.
Save adyngom/f45e00018fdda33ef15dc31a5324413a to your computer and use it in GitHub Desktop.
Max difference in a set of numbers
const nums = [2, 4, 7, 5, 13, 11, 9, 1, 6, 2];
console.log(getMaxDifference(nums));
function getMaxDifference(arr) {
let max = arr[0], min = arr[0];
[...arr].forEach(n => {
if(n > max) max = n;
if(n < min) min = n;
});
return max - min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment