const range = (...myData: number[]) => {
myData.sort();
return myData[myData.length-1] - myData[0];
}
Codecademy says "Although the type for the rest parameter is an array of numbers, calling range() with no argument is perfectly valid and generates no TypeScript error. However, a value of NaN will be returned."
I thought TypeScript gives an error if we don’t provide a value for the arguments of a function, unless they have a ? after their name.
...myData: number[]means zero or more numbers, each as separate parameters, not 1 or more. Are you looking for ways to change the type to force there to be at least 1 number? or are you looking to modify the code to return something other thanNaNwhen given zero?range(), in which casemyData: number[] = [] -> myData.length === 0), or they can contain values (i.e. one of more arguments at the point of the parameter, e.g.range(1, 2, 3), in which casemyData: number[] = [1, 2, 3] -> myData.length === 3). The reasonrange()returnsNaNis because:myData[-1] - myData[0] -> undefined - undefined === NaN