Challenge - 5 Problems
Default Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with default typed parameters
What is the output of this TypeScript function when called as
greet()?Typescript
function greet(name: string = "Guest", age: number = 30): string { return `Hello, ${name}! You are ${age} years old.`; } console.log(greet());
Attempts:
2 left
💡 Hint
Check the default values assigned to the parameters in the function signature.
✗ Incorrect
The function greet has default values for both parameters. When called without arguments, it uses these defaults and returns the greeting with "Guest" and 30.
❓ Predict Output
intermediate2:00remaining
Effect of overriding default parameters
What will be the output when calling
calculateArea(5) with this function?Typescript
function calculateArea(width: number, height: number = width): number { return width * height; } console.log(calculateArea(5));
Attempts:
2 left
💡 Hint
Look at how the default value for height depends on width.
✗ Incorrect
The height parameter defaults to the value of width if not provided. So calculateArea(5) computes 5 * 5 = 25.
🔧 Debug
advanced2:00remaining
Identify the error with default parameter types
What error does this TypeScript code produce?
Typescript
function multiply(a: number = 2, b: number): number { return a * b; } console.log(multiply(undefined, 3));
Attempts:
2 left
💡 Hint
Check the order of parameters with and without default values.
✗ Incorrect
In TypeScript, parameters with default values must come after required parameters. Here, 'a' has a default but comes before 'b' which is required, causing a compile-time error.
❓ Predict Output
advanced2:00remaining
Output with complex default parameter expressions
What is the output of this code?
Typescript
function buildName(firstName: string, lastName: string = firstName.toUpperCase()): string { return `${firstName} ${lastName}`; } console.log(buildName("john"));
Attempts:
2 left
💡 Hint
The default value for lastName uses firstName's value.
✗ Incorrect
The lastName defaults to firstName converted to uppercase. So calling buildName("john") returns "john JOHN".
🧠 Conceptual
expert2:00remaining
Understanding default parameter evaluation timing
Consider this TypeScript code. What will be the output when
foo() is called?Typescript
let x = 10; function foo(a: number = x) { console.log(a); } x = 20; foo();
Attempts:
2 left
💡 Hint
Default parameters are evaluated at call time, not at function definition time.
✗ Incorrect
The default parameter 'a' uses the current value of 'x' when foo() is called. Since x was changed to 20 before calling foo(), it prints 20.