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.ts180 chars7 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.ts30 chars2 lines
gistlibby LogSnag