0
0
Typescriptprogramming~20 mins

String enums in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Enum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A"LEFT"
BLeft
C3
Dundefined
Attempts:
2 left
💡 Hint
Remember that string enums store string values, not numbers.
Predict Output
intermediate
2: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);
A1
B"PENDING"
Cundefined
DSyntaxError
Attempts:
2 left
💡 Hint
In string enums, all members must have string initializers.
🧠 Conceptual
advanced
2: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?
AString enums provide better readability and debugging because values are descriptive strings.
BString enums are faster to execute than numeric enums.
CString enums allow automatic reverse mapping from value to key.
DString enums consume less memory than numeric enums.
Attempts:
2 left
💡 Hint
Think about how values appear in logs and error messages.
🔧 Debug
advanced
2: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);
ANo error, outputs "RED"
BReferenceError: Colors is not defined
CType '"RED"' is not assignable to type 'Colors'.
DTypeError: favorite is not a Colors enum
Attempts:
2 left
💡 Hint
Check the type compatibility between string literals and enum types.
🚀 Application
expert
2: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);
A6
B3
C0
Dundefined
Attempts:
2 left
💡 Hint
String enums do not create reverse mappings like numeric enums.