Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function dayOfWeek(input) {
- let num = parseInt(input[0]);
- if (num == 1)
- console.log('Monday');
- else if (num == 2)
- console.log('Tuesday');
- else if (num == 3)
- console.log('Wednesday');
- else if (num == 4)
- console.log('Thursday');
- else if (num == 5)
- console.log('Friday');
- else if (num == 6)
- console.log('Saturday');
- else if (num == 7)
- console.log('Sunday');
- else
- console.log('Error');
- }
- Второ решение (switch):
- function dayOfWeek(input) {
- let num = parseInt(input[0]);
- switch (num) {
- case 1:
- console.log('Monday');
- break;
- case 2:
- console.log('Tuesday');
- break;
- case 3:
- console.log('Wednesday');
- break;
- case 4:
- console.log('Thursday');
- break;
- case 5:
- console.log('Friday');
- break;
- case 6:
- console.log('Saturday');
- break;
- case 7:
- console.log('Sunday');
- break;
- default:
- console.log('Error');
- break;
- }
- }
- Решение с тернарен оператор:
- function dayOfWeek(input) {
- let num = parseInt(input[0]);
- console.log(
- num == 1 ? "Monday" :
- num == 2 ? "Tuesday" :
- num == 3 ? "Wednesday" :
- num == 4 ? "Thursday" :
- num == 5 ? "Friday" :
- num == 6 ? "Saturday" :
- num == 7 ? "Sunday" : "Error"
- );
- }
- Решение с колекция и тернарен оператор:
- function dayOfWeek(input) {
- let num = parseInt(input[0]);
- let dayOfWeek = ["Error", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
- console.log(dayOfWeek[(num > 0 && num < dayOfWeek.length ? num : 0)]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement