0
0
Typescriptprogramming~15 mins

Numeric enums in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Numeric enums
📖 Scenario: Imagine you are creating a simple program to represent the days of the week using numbers. Each day will have a number starting from 0 for Sunday, 1 for Monday, and so on.
🎯 Goal: You will create a numeric enum called Days for the days of the week, then write code to get the number for Wednesday and print it.
📋 What You'll Learn
Create a numeric enum called Days with days Sunday to Saturday starting at 0
Create a variable called midWeek and assign it the value of Days.Wednesday
Print the value of midWeek to the console
💡 Why This Matters
🌍 Real World
Numeric enums are useful when you want to represent a fixed set of related constants with numbers, like days, months, or status codes.
💼 Career
Understanding enums helps in writing clear and maintainable code in TypeScript, which is widely used in web development and software engineering jobs.
Progress0 / 4 steps
1
Create the numeric enum
Create a numeric enum called Days with these exact members and values: Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6.
Typescript
Need a hint?

Use the enum keyword followed by the name Days. Assign numbers starting at 0 to each day.

2
Assign a variable from the enum
Create a variable called midWeek and assign it the value Days.Wednesday.
Typescript
Need a hint?

Use let midWeek = Days.Wednesday; to assign the enum value.

3
Use the enum value in a function
Create a function called isMidWeek that takes a parameter day of type Days and returns true if day equals Days.Wednesday, otherwise false. Then call isMidWeek with midWeek and assign the result to a variable called check.
Typescript
Need a hint?

Write a function that compares the input day to Days.Wednesday and returns true or false.

4
Print the result
Print the value of midWeek and the value of check to the console using two separate console.log statements.
Typescript
Need a hint?

Use console.log(midWeek); and console.log(check); to print the values.