I'm struggling with an exercise involving Function parameters:
The maximum function below has the wrong type. To allow undefined in the rest arguments, you need to update the type of the rest parameter. Fortunately, you don't have to change the function body since the if conditional already ignores undefined values.
function maximum(...numbers: Array<number>) {
let max = -Infinity;
for (const n of numbers) {
if (n !== undefined && n > max) {
max = n;
}
}
return max;
}
I attempted to make changes as shown below but still ended up with incorrect results. Are there any alternative ways to modify the type so that it accepts undefined?
function maximum(...numbers: Array<number> | undefined) {
let max = -Infinity;
for(const n of numbers) {
if(n !== undefined && n > max) {
max = n;
}
}
return max;
}