forked from leelichtsinn/example-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic-examples.js
More file actions
35 lines (31 loc) · 1010 Bytes
/
logic-examples.js
File metadata and controls
35 lines (31 loc) · 1010 Bytes
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
// Q. when do you use multiple if statements?
// A. when you want to run multiple statements at a time
// if it meets multiple / all of the conditionals
function fizzbuzz(x) {
var string = '';
if (x % 3 === 0) {
string += 'fizz';
}
if (x % 5 === 0) {
string += 'buzz';
}
// when x is both divisble by 3 AND divisible by 5, both statements will run
// --> string would be 'fizzbuzz' when it meets both conditions
return string || x; // when string is empty (''), it will evalute to FALSE and return x
}
// if-else-if-elseif-....-else will cause only ONE statement to run
// and does not allow for multiple
function fizzbuzz2(x) {
if (x % 3 === 0) {
return 'fizz';
} else if (x % 5 === 0) {
return 'buzz';
} else if (x % 3 === 0 && x % 5 === 0) {
return 'fizzbuzz';
} else {
return x;
}
}
// notice above that on evaluating fizzbuzz2(x),
// only ONE of those statements will ever be called
// multiple if statements allow for multiple statements to be called