-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitchStatements.js
More file actions
106 lines (94 loc) · 1.96 KB
/
switchStatements.js
File metadata and controls
106 lines (94 loc) · 1.96 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
/* Switch Statement
The switch statement is used to evaluate an expression then
associating it with a case clause and finally running code executing
statements matching that case
*/
// 1. The Switch expression is evaluated once.
// 2. The value of the expression is compared with values of each case.
// 3. If there is a match, the associated block of code is executed.
var gradeRemark = 'B';
switch(gradeRemark){
case 'A':
alert('Great job!');
break;
case 'B':
alert('Good shit');
break;
default:
alert('You are grounded');
}
// Case B GOOD SHIT
// review switch statement
// expression, case run code for match
// break
// default
/*
syntax
switch(expression) {
case a:
code block;
break;
case b:
code block;
break;
case c:
code block;
break;
default:
code block;
}
*/
// Challenge
// Create switch with evaluation balue between 1-6
// Have code block for each possible match to run
// End with default code block
// Expression (1-6) can be hard coded by you or you can create method for random number
var dice = 6;
switch(dice) {
case 1:
alert ('Terrible roll');
break;
case 2:
alert ('you suck, next');
break;
case 3:
alert ('damn dawg, nope');
break;
case 4 :
alert ('faaak');
break;
case 5:
alert ('close but no');
break;
case 6:
alert ('Yes, finally you got 6');
break;
default:
alert ('My g did you roll');
break;
}
// Make dice roll random with (Math.floor(Math.random())
var dice = 6;
switch(Math.floor(Math.random() * 7)) {
case 1:
alert ('Terrible roll');
break;
case 2:
alert ('you suck, next');
break;
case 3:
alert ('damn dawg, nope');
break;
case 4 :
alert ('faaak');
break;
case 5:
alert ('close but no');
break;
case 6:
alert ('Yes, finally you got 6');
break;
default:
alert ('My g did you roll');
break;
}