find the range of a list of numbers in typescript

To find the range of a list of numbers in TypeScript, you can use the spread syntax ... to convert the array elements into arguments, then apply the Math.min() and Math.max() methods to get the minimum and maximum value respectively. Finally, subtract the minimum from the maximum to get the range.

Here's a code snippet that demonstrates this:

index.ts
const numbers = [5, 2, 9, 1, 7];
const min = Math.min(...numbers);
const max = Math.max(...numbers);
const range = max - min;

console.log(`The range of the numbers is ${range}`);
180 chars
7 lines

In this example, the numbers array contains 5 elements. We first use Math.min() to find the smallest value, which is 1. Then, we use Math.max() to find the largest value, which is 9. Finally, we subtract the smallest value from the largest value to get the range, which is 8. The output of this code will be:

index.ts
The range of the numbers is 8
30 chars
2 lines

related categories

gistlibby LogSnag