Challenge - 5 Problems
Typed Functions 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 typed function?
Consider this TypeScript function with explicit types. 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 the function return type and how the argument is used.
✗ Incorrect
The function greet takes a string and returns a greeting string. The call greet('Alice') returns 'Hello, Alice!'.
🧠 Conceptual
intermediate1:00remaining
Why do typed functions help catch errors early?
Which of these best explains why typed functions are useful in TypeScript?
Attempts:
2 left
💡 Hint
Think about when type errors are found.
✗ Incorrect
Typed functions let TypeScript check if the right types are used before running the program, catching errors early.
🔧 Debug
advanced1:30remaining
What error does this typed function cause?
Look at this TypeScript function. What error will the compiler show?
Typescript
function add(a: number, b: number): number { return a + b + '5'; }
Attempts:
2 left
💡 Hint
Check the return type and what is being returned.
✗ Incorrect
The function is declared to return a number but returns a number plus a string, which results in a string. TypeScript flags this as a type error.
📝 Syntax
advanced1:30remaining
Which typed function syntax is correct?
Which option shows a correct TypeScript function with typed parameters and return type?
Attempts:
2 left
💡 Hint
Check parameter and return type annotations.
✗ Incorrect
Option D correctly types both parameters and the return value as numbers.
🚀 Application
expert2:00remaining
What is the output of this typed function using union types?
What will this TypeScript function print when called with
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
Look at how the function handles number and string types differently.
✗ Incorrect
The function checks the type of id. For number, it formats without decimals. For string, it converts to uppercase.