0
0
Typescriptprogramming~20 mins

Numeric enums in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Numeric Enum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numeric enum auto-increment
What is the output of the following TypeScript code when compiled and run in JavaScript?
Typescript
enum Direction {
  Up,
  Down,
  Left,
  Right
}
console.log(Direction.Up, Direction.Down, Direction.Left, Direction.Right);
A1 2 3 4
B0 1 2 3
CUp Down Left Right
Dundefined undefined undefined undefined
Attempts:
2 left
💡 Hint
Numeric enums start numbering from 0 by default unless specified.
Predict Output
intermediate
2:00remaining
Custom starting value in numeric enum
What will this TypeScript code output when run?
Typescript
enum Status {
  Ready = 5,
  Waiting,
  Complete
}
console.log(Status.Ready, Status.Waiting, Status.Complete);
A0 1 2
B5 5 5
C5 6 7
Dundefined undefined undefined
Attempts:
2 left
💡 Hint
When you assign a number to the first member, the next members increment from that number.
🔧 Debug
advanced
2:00remaining
Why does this enum cause an error?
This TypeScript enum code causes a compilation error. What is the reason?
Typescript
enum Colors {
  Red = 1,
  Green = Red + "2",
  Blue
}
AEnum members must be initialized with constant values or previous enum members without expressions.
BYou cannot use string concatenation in numeric enum values.
CBlue must be assigned a value explicitly after Green uses an expression.
DEnums cannot mix string and numeric values.
Attempts:
2 left
💡 Hint
Enum values must be constant expressions for numeric enums.
Predict Output
advanced
2:00remaining
Reverse mapping of numeric enums
What is the output of this TypeScript code?
Typescript
enum Weekday {
  Monday = 1,
  Tuesday,
  Wednesday
}
console.log(Weekday[2]);
Aundefined
B2
C"Monday"
D"Tuesday"
Attempts:
2 left
💡 Hint
Numeric enums create a reverse mapping from value to name.
🧠 Conceptual
expert
2:00remaining
Number of enum members after compilation
Given this TypeScript enum, how many keys will the compiled JavaScript object have?
Typescript
enum Levels {
  Low = 1,
  Medium = 3,
  High
}
A6
B3
C5
D4
Attempts:
2 left
💡 Hint
Remember that numeric enums create both forward and reverse mappings for each member.