0
0
Typescriptprogramming~20 mins

Why enums are needed in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this enum usage?
Consider this TypeScript code using an enum. What will be printed to the console?
Typescript
enum Direction {
  Up = 1,
  Down,
  Left,
  Right
}

console.log(Direction.Up);
console.log(Direction[2]);
A2 and 'Down'
B1 and 'Down'
C1 and 'Up'
D0 and 'Up'
Attempts:
2 left
💡 Hint
Remember enums auto-increment numeric values starting from the first assigned value.
🧠 Conceptual
intermediate
1:30remaining
Why use enums instead of plain constants?
Which of the following is the main reason to use enums in TypeScript instead of just using plain constants or strings?
AEnums group related values and provide readable names with automatic numbering.
BEnums make the code run faster than constants.
CEnums allow you to use variables without declaring them first.
DEnums automatically convert strings to numbers in all cases.
Attempts:
2 left
💡 Hint
Think about how enums help organize related values.
🔧 Debug
advanced
2:00remaining
What error does this enum code produce?
What error will this TypeScript code cause when compiled or run?
Typescript
enum Status {
  Active = 'ACTIVE',
  Inactive = 'INACTIVE',
  Pending
}

console.log(Status.Pending);
AOutput: 2
BOutput: 'PENDING'
CError: Enum member must have initializer because previous member is a string
DNo error, outputs undefined
Attempts:
2 left
💡 Hint
Check how string enums require explicit values for all members.
🚀 Application
advanced
1:30remaining
How many items are in this enum object?
Given this enum, how many keys does the compiled JavaScript object have?
Typescript
enum Colors {
  Red,
  Green,
  Blue
}
A6
B0
C3
D9
Attempts:
2 left
💡 Hint
Remember TypeScript enums create reverse mappings for numeric values.
Predict Output
expert
2:30remaining
What is the output of this mixed enum code?
What will this TypeScript code print to the console?
Typescript
enum Mixed {
  A = 1,
  B = 'B',
  C = 3
}

console.log(Mixed.A);
console.log(Mixed.B);
console.log(Mixed.C);
console.log(Mixed['B']);
A1, 'B', 3, undefined
B1, 'B', 3, 1
CError at enum declaration
D1, 'B', 3, 'B'
Attempts:
2 left
💡 Hint
Check how mixed enums handle reverse mapping for string members.