Challenge - 5 Problems
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of reverse mapping in numeric enum
What is the output of the following TypeScript code when compiled to JavaScript and run?
Typescript
enum Colors {
Red = 1,
Green,
Blue
}
console.log(Colors[2]);Attempts:
2 left
💡 Hint
Remember numeric enums create a reverse mapping from value to name.
✗ Incorrect
In numeric enums, each numeric value maps back to its name. Since Green has value 2, Colors[2] returns "Green".
❓ Predict Output
intermediate2:00remaining
Reverse mapping with custom numeric values
What will be logged by this code snippet?
Typescript
enum Status {
Pending = 5,
Active = 10,
Completed = 15
}
console.log(Status[10]);Attempts:
2 left
💡 Hint
Check which enum member has the value 10.
✗ Incorrect
The enum member Active has the value 10, so Status[10] returns "Active".
🧠 Conceptual
advanced2:00remaining
Why reverse mapping does not exist for string enums
Why does reverse mapping not work for string enums in TypeScript?
Attempts:
2 left
💡 Hint
Think about how TypeScript compiles numeric vs string enums.
✗ Incorrect
TypeScript generates reverse mapping only for numeric enums because numeric values can be used as keys. String enums do not have reverse mapping generated.
❓ Predict Output
advanced2:00remaining
Output when accessing reverse mapping with invalid key
What is the output of this code?
Typescript
enum Directions {
Up = 1,
Down,
Left,
Right
}
console.log(Directions[5]);Attempts:
2 left
💡 Hint
Check if the enum has a member with value 5.
✗ Incorrect
No enum member has the value 5, so Directions[5] returns undefined.
🧠 Conceptual
expert3:00remaining
How reverse mapping affects enum object size
How does reverse mapping in numeric enums affect the size of the compiled enum object?
Attempts:
2 left
💡 Hint
Think about how many keys exist in the compiled enum object compared to the number of enum members.
✗ Incorrect
Numeric enums generate both name-to-value and value-to-name mappings, effectively doubling the number of properties in the compiled object.