Challenge - 5 Problems
Rest Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function using typed rest parameters
What is the output of this TypeScript code when calling
sumAll(1, 2, 3, 4)?Typescript
function sumAll(...nums: number[]): number { return nums.reduce((a, b) => a + b, 0); } console.log(sumAll(1, 2, 3, 4));
Attempts:
2 left
💡 Hint
The rest parameter collects all arguments into an array of numbers.
✗ Incorrect
The function sums all numbers passed as arguments using reduce. The output is the sum 1+2+3+4 = 10.
❓ Predict Output
intermediate2:00remaining
Output of function with mixed typed rest parameters
What will this TypeScript code output when calling
concatStrings('a', 'b', 'c')?Typescript
function concatStrings(separator: string, ...strings: string[]): string { return strings.join(separator); } console.log(concatStrings('-', 'a', 'b', 'c'));
Attempts:
2 left
💡 Hint
The first argument is the separator, the rest are joined with it.
✗ Incorrect
The function uses the first argument as separator and joins the rest strings with it, producing 'a-b-c'.
🔧 Debug
advanced2:00remaining
Identify the error with rest parameter typing
What error does this TypeScript code produce?
Typescript
function multiplyAll(...nums: number): number { return nums.reduce((a, b) => a * b, 1); }
Attempts:
2 left
💡 Hint
Check the type of the rest parameter declaration.
✗ Incorrect
Rest parameters must be typed as arrays (e.g., number[]). Using just 'number' is invalid syntax.
❓ Predict Output
advanced2:00remaining
Output of function with tuple typed rest parameters
What is the output of this TypeScript code?
Typescript
function formatName(first: string, last: string, ...titles: string[]): string { return `${first} ${last} ${titles.join(' ')}`.trim(); } console.log(formatName('John', 'Doe', 'PhD', 'MD'));
Attempts:
2 left
💡 Hint
The rest parameter collects all extra titles as an array.
✗ Incorrect
The function concatenates first and last names with all titles joined by spaces, resulting in 'John Doe PhD MD'.
🧠 Conceptual
expert2:00remaining
TypeScript rest parameter with tuple types
Given this function signature, what is the type of the rest parameter
args?
function example(...args: [number, string, boolean]): void;
Attempts:
2 left
💡 Hint
Tuple types specify fixed length and types in order.
✗ Incorrect
The rest parameter typed as a tuple means exactly three arguments must be passed: number, string, boolean in that order.