find the range of a list of numbers in javascript

To find the range of a list of numbers in JavaScript, you can use the Math.max() and Math.min() functions.

index.tsx
let numbers = [5, 10, 15, 20, 25];
let max = Math.max(...numbers);
let min = Math.min(...numbers);
let range = max - min;

console.log("Range:", range); // expected output: Range: 20
183 chars
7 lines

First, we define an array of numbers. We can use the spread operator (...) to pass the numbers as separate arguments to the Math.max() and Math.min() functions. We then subtract the minimum value from the maximum to get the range of the numbers, which we store in the range variable.

In this example, the range of the numbers [5, 10, 15, 20, 25] is 20.

gistlibby LogSnag