Use a numeric enum to turn day numbers into names and decide which days are at the weekend.
What to build
A program that names each day number.
Requirements
- Declare
enum WeekdaywithMonday = 1up toSunday = 7. - For each number print
N: NAME (weekday)orN: NAME (weekend); Saturday and Sunday are the weekend. - For a number with no day print
N: no such day.
Input
Line 1: n. Then n whole numbers, one per line.
Output
n lines.
Constraints
1 ≤ n ≤ 50; -100 ≤ each number ≤ 100.
Example
Input:
3
1
6
9
Output:
1: Monday (weekday)
6: Saturday (weekend)
9: no such day
Why it works
A numeric enum stores both directions, so Weekday[6] gives "Saturday" and Weekday.Saturday gives 6, and comparing with the members reads better than comparing with bare numbers.
Examples
Example 1 · Input
3 1 6 9
Expected output
1: Monday (weekday) 6: Saturday (weekend) 9: no such day
Example 2 · Input
2 7 5
Expected output
7: Sunday (weekend) 5: Friday (weekday)
Hints
Hint 1
enum Weekday { Monday = 1, Tuesday, ... } numbers the rest automatically.
Hint 2
Weekday[n] gives the name back for a numeric enum, or undefined for a number that is not a member.
Solution
Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";
enum Weekday {
Monday = 1,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
const n = Number(read());
for (let i = 0; i < n; i++) {
const day = Number(read());
const name: string | undefined = Weekday[day];
if (name === undefined) {
console.log(day + ": no such day");
} else {
const weekend = day === Weekday.Saturday || day === Weekday.Sunday;
console.log(day + ": " + name + (weekend ? " (weekend)" : " (weekday)"));
}
}