Challenge - 5 Problems
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput 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
intermediateReverse 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
advancedWhy 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
advancedOutput 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
expertHow 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.
