-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif-else-if-vs-switch-case.js
58 lines (51 loc) · 1.15 KB
/
if-else-if-vs-switch-case.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
'use strict'
import { runProcess } from '../utils/helpers.js';
const animals = ['horse', 'rabbit', 'cat', 'dog', 'bear', 'sheep'];
export function runIfElseIfVsSwitchCaseProcess() {
console.log('\nStarting if-else-if ladder vs switch-case test.....');
runProcess(usingIfElseIfLadder, usingSwitchCase);
console.log('Process finished!\n');
}
function usingIfElseIfLadder() {
let count = 0;
for (let animal of animals) {
if (animal === 'horse') {
count += 5;
} else if (animal === 'rabbit') {
count *= 3;
} else if (animal === 'cat') {
count -= 7;
} else if (animal === 'dog') {
count /= 2;
} else if (animal === 'bear') {
count++;
} else {
count *= count;
}
}
}
function usingSwitchCase() {
let count = 0;
for (let animal of animals) {
switch (animal) {
case 'horse':
count += 5;
break;
case 'rabbit':
count *= 3;
break;
case 'cat':
count -= 7;
break;
case 'dog':
count /= 2;
break;
case 'bear':
count++;
break;
default:
count *= count;
break;
}
}
}