Challenge - 5 Problems
String Enum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing string enum member
What is the output of this TypeScript code?
Typescript
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
console.log(Direction.Left);Attempts:
2 left
💡 Hint
Remember that string enums store string values, not numbers.
✗ Incorrect
The enum member Direction.Left is assigned the string "LEFT" explicitly, so logging it outputs "LEFT".
❓ Predict Output
intermediate2:00remaining
Value of enum member without explicit assignment
What will be the output of this TypeScript code?
Typescript
enum Status {
Pending = "PENDING",
Active,
Completed = "COMPLETED"
}
console.log(Status.Active);Attempts:
2 left
💡 Hint
In string enums, all members must have string initializers.
✗ Incorrect
In TypeScript, string enums require all members to have string values. Omitting the value for Active causes a syntax error.
🧠 Conceptual
advanced2:00remaining
Why use string enums over numeric enums?
Which of the following is the best reason to use string enums instead of numeric enums in TypeScript?
Attempts:
2 left
💡 Hint
Think about how values appear in logs and error messages.
✗ Incorrect
String enums use descriptive string values which make code easier to read and debug compared to numeric enums which use numbers.
🔧 Debug
advanced2:00remaining
Identify the error in this string enum usage
What error will this TypeScript code produce?
Typescript
enum Colors {
Red = "RED",
Green = "GREEN",
Blue = "BLUE"
}
const favorite: Colors = "RED";
console.log(favorite);Attempts:
2 left
💡 Hint
Check the type compatibility between string literals and enum types.
✗ Incorrect
You cannot assign a string literal directly to a variable typed as a string enum. The value must be Colors.Red.
🚀 Application
expert2:00remaining
Count enum members with string values
Given this string enum, how many members does it have?
Typescript
enum Fruits {
Apple = "APPLE",
Banana = "BANANA",
Cherry = "CHERRY"
}
const count = Object.keys(Fruits).length;
console.log(count);Attempts:
2 left
💡 Hint
String enums do not create reverse mappings like numeric enums.
✗ Incorrect
String enums only have keys for their members, so Object.keys returns the member names only, count is 3.