Challenge - 5 Problems
Type Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
What is the output of this function call?
Consider this TypeScript function with parameter type annotations. What will be printed when calling
greet('Alice')?Typescript
function greet(name: string): string { return `Hello, ${name}!`; } console.log(greet('Alice'));
Attempts:
2 left
💡 Hint
Look at how the function returns a string and how console.log prints it.
✗ Incorrect
The function returns a string with the name inserted. console.log prints the string without quotes.
❓ Predict Output
intermediate1:30remaining
What error does this code produce?
What error will this TypeScript code cause when compiled?
Typescript
function multiply(x: number, y: number): number { return x * y; } multiply('2', 3);
Attempts:
2 left
💡 Hint
Check the types of arguments passed to the function.
✗ Incorrect
The function expects numbers, but a string is passed, causing a compile-time type error.
🔧 Debug
advanced2:00remaining
Why does this function cause a compile error?
This function is intended to add two numbers. Why does TypeScript report an error?
Typescript
function add(a, b: number): number { return a + b; }
Attempts:
2 left
💡 Hint
Check the types of all parameters.
✗ Incorrect
Parameter 'a' lacks a type annotation, so TypeScript treats it as 'any' which may be disallowed depending on settings.
❓ Predict Output
advanced1:30remaining
What is the output of this function with default parameter and type annotation?
What will this code print?
Typescript
function welcome(name: string = 'Guest'): string { return `Welcome, ${name}!`; } console.log(welcome());
Attempts:
2 left
💡 Hint
Look at the default value for the parameter.
✗ Incorrect
The parameter has a default value 'Guest', so calling without arguments uses that.
❓ Predict Output
expert2:30remaining
What is the output of this function using union types in parameters?
What will this code print when calling
formatId(123) and formatId('abc')?Typescript
function formatId(id: number | string): string { if (typeof id === 'number') { return `ID-${id.toFixed(0)}`; } else { return `ID-${id.toUpperCase()}`; } } console.log(formatId(123)); console.log(formatId('abc'));
Attempts:
2 left
💡 Hint
Check how the function handles number and string types differently.
✗ Incorrect
For number, toFixed(0) converts to string without decimals; for string, toUpperCase converts to uppercase.