-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#8ConditionalStatement--switch.js
More file actions
116 lines (105 loc) · 2.92 KB
/
#8ConditionalStatement--switch.js
File metadata and controls
116 lines (105 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Instructions
In JavaScript, switch can replace multiple if statements.
switch(n){
case 1:
//code block
break;
case 2:
//code block
break;
case ...:
//code block
break;
default: //default is optional, sometimes it can be omitted
//code block
//The last one does not need break
}
switch is the keyword and n is the variable to switch. case 1 means when n===1. Following the ":" you can add your code for what to do in that case. The keyword break is used as termination - if you forget break, the code will continue running through the other case statements and default until a break appears. If no case statements match, default is entered.
In some instances, the switch statement is clearer than the if..else statement.
For example, we can write a function to calculate what day today is, like this:
function whatDayIsToday(n){
// getDay() is a method of Date() - we will learn this later
var day=new Date().getDay(),x;
switch (day){
case 0:
x="Today it's Sunday";
break;
case 1:
x="Today it's Monday";
break;
case 2:
x="Today it's Tuesday";
break;
case 3:
x="Today it's Wednesday";
break;
case 4:
x="Today it's Thursday";
break;
case 5:
x="Today it's Friday";
break;
case 6:
x="Today it's Saturday";
break;
}
return x;
}
Task
Complete the function howManydays. It accepts 1 parameter month, which means the month of the year. Different months have a different number of days as shown in the table below. Return the number of days that are in month. There is no need for input validation: month will always be greater than 0 and less than or equal to 12.
+---------------+-------------+
| month | days |
+---------------+-------------+
|1,3,5,7,8,10,12| 31 |
+---------------+-------------+
|4,6,9,11 | 30 |
+---------------+-------------+
|2 | 28 | (Do not consider the leap year)
+---------------+-------------+
Tip: Using default for most of the cases can reduce your work.
When you have finished, click "Test" to test your code against initial tests and "Attempt" to test your code against all tests. If you pass in all tests you can click "Submit" to submit your code.
*/
//Answer
//P.R.E.P Parameters, Returns, Examples, Pseudo Code
//P: string
//R: days
//E: howManydays(3) = returns 31
//P: feb = 28 days
function howManydays(month) {
var days;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = 28;
break;
}
return days;
}
// Better practice
function howManydays(month) {
switch (month) {
case 2:
return 28;
break;
case 4:
case 6:
case 9:
case 11:
return 30;
}
return 31;
}