Highest and lowest value difference of array JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in an array of numbers. The function should pick the greatest and lowest value from the array and return their difference.

Example

const arr = [4, 6, 3, 1, 5, 8, 9, 3, 4];
const difference = (arr = []) => {
   const highest = Math.max(...arr);
   const lowest = Math.min(...arr);
   return highest - lowest;
};
console.log(difference(arr));

Output

And the output in the console will be −

8
raja
Published on 25-Nov-2020 08:09:27
Advertisements