-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditionals.js
186 lines (154 loc) · 4.28 KB
/
conditionals.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//In order for JavaScript to compare two different data types (for example, numbers and strings), it must convert one type to another (Type Coercion).
function equalityTest(val1, val2) {
if (val1 == val2) {
return "Equal";
}
return "Not equal";
}
console.log(equalityTest(1, 1)); // Equal
console.log(equalityTest(1, 2)); // Not equal
console.log(equalityTest(1, "1")); // Equal
console.log(equalityTest('3', 3)); // Equal
/*
Strict (in)equality (=== and !==) is the counterpart to the (in)equality operator (== != >< >= <=).
However, unlike the equality operator, which attempts to convert both values being compared to a common type,
the strict equality operator does not perform a type conversion.
*/
function strictEqualityTest(val1, val2) {
if (val1 === val2) {
return "Equal";
}
return "Not equal";
}
onsole.log(equalityTest(1, 1)); // Equal
console.log(equalityTest(1, 2)); // Not equal
console.log(equalityTest(1, "1")); // Not equal
console.log(equalityTest('3', 3)); // Not equal
typeof 3 //returns 'number'
typeof '3' //returns 'string'
// Comparisons with the logical operators
// && = AND
function testLogicalAnd(val) {
if (val <=50 && val >= 25) {
return "Yes";
}
return "No";
}
testLogicalAnd(30);
testLogicalAnd(3);
// || = OR
function testLogicalOr(val) {
if (val < 10 || val > 20) {
return "Outside";
}
return "Inside";
}
testLogicalOr(15);
testLogicalOr(155);
// ELSE
function testElse(val) {
var result = "";
if (val > 5) {
result = "Bigger than 5";
} else {
result = "5 or Smaller";
}
return result;
}
testElse(4);
// ELSE IF
function testElseIf(val) {
if (val > 10) {
return "Greater than 10";
} else if (val < 5) {
return "Smaller than 5";
} else {
return "Between 5 and 10";
}
}
testElseIf(7);
// Selecting from Many Options with Switch Statements
/* If you have many options to choose from, use a switch statement. A switch statement tests a value and can have many case statements
which define various possible values. Statements are executed from the first matched case value until a break is encountered.
case values are tested with strict equality (===). The break tells JavaScript to stop executing statements.
If the break is omitted, the next statement will be executed.*/
function caseInSwitch(val) {
var answer = "";
switch(val) {
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta"
break;
}
return answer;
}
// remember about BREAK; after each set of statements
console.log(caseInSwitch(1)); //"alpha"
/* In a switch statement you may not be able to specify all possible values as case statements.
Instead, you can add the default statement which will be executed if no matching case statements are found.
Think of it like the final else statement in an if/else chain.*/
function switchOfStuff(val) {
var answer = "";
switch (val) {
case "a":
answer = "apple";
break;
case "b":
answer = "bird";
break;
case "c":
answer = "cat";
break;
default:
answer = "stuff";
break;
}
return answer;
}
console.log(switchOfStuff(1)); // "stuff"
// Multiple Identical Options in Switch Statements
// If the break statement is omitted from a switch statement's case, the following case statement(s) are executed until a break is encountered.
function sequentialSizes(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
answer = "Low";
break;
case 4:
case 5:
case 6:
answer = "Mid";
break;
case 7:
case 8:
case 9:
answer = "High";
break;
}
return answer;
}
console.log(sequentialSizes(1)); //"Low"
// Conditional (Ternary) Operator
/* The syntax is a ? b : c, where
a is the condition,
b is the code to run when the condition returns true,
and c is the code to run when the condition returns false. */
function findGreater(a, b) {
return a > b ? "a is greater" : "b is greater";
}
// You can chain them up similarily to else...else if...else statements
function checkSign(num) {
return (num > 0) ? "positive"
: (num < 0) ? "negative"
: "zero";
} // N.B. Remember that the ES6 shorthand conditional should be within the return statement