Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a function with rest parameters typed as numbers.
Typescript
function sum(...numbers: [1]) { return numbers.reduce((a, b) => a + b, 0); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' instead of a specific type.
Forgetting to use array syntax '[]' for rest parameters.
✗ Incorrect
The rest parameter 'numbers' should be typed as an array of numbers, which is 'number[]'.
2fill in blank
mediumComplete the code to type the rest parameter as an array of strings.
Typescript
function greetAll(...greetings: [1]) { greetings.forEach(greet => console.log(greet)); } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' instead of 'string[]'.
Using 'any[]' which is less specific.
✗ Incorrect
Rest parameters must be typed as arrays, so 'string[]' is the correct type for an array of strings.
3fill in blank
hardFix the error by correctly typing the rest parameter as an array of booleans.
Typescript
function checkFlags(...flags: [1]) { return flags.every(flag => flag === true); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'boolean' instead of 'boolean[]'.
Using 'any' which is not specific.
✗ Incorrect
Rest parameters must be typed as arrays, so 'boolean[]' is the correct type for an array of booleans.
4fill in blank
hardFill both blanks to declare a function with rest parameters typed as an array of numbers and return their product.
Typescript
function multiplyAll(...[1]: [2]) { return [1].reduce((a, b) => a * b, 1); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string array type instead of number array.
Forgetting to name the rest parameter.
✗ Incorrect
The rest parameter name can be 'nums' and its type should be 'number[]' to represent an array of numbers.
5fill in blank
hardFill all three blanks to declare a function with rest parameters typed as strings, filter those longer than 3 characters, and return the filtered array.
Typescript
function filterLongStrings(...[1]: [2]): [2] { return [1].filter(str => str.length > 3); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number[]' or 'any[]' instead of 'string[]'.
Mismatching parameter and return types.
✗ Incorrect
The rest parameter name is 'words', typed as 'string[]'. The function returns the same type 'string[]'.