-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-EJS-Ch2-program-structure.js
188 lines (169 loc) · 3.86 KB
/
03-EJS-Ch2-program-structure.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
187
var assert = require('assert');
var ___ = undefined;
/*
* To complete the exercise, replace underscores in the
* code below with appropriate values to make the tests
* pass.
*
* See README.md for installing the test library and
* running the tests.
*/
// I'm skipping over variables, because you know this stuff already...
describe("Variables", function(){});
describe("[Control flow]: ", function(){
describe("The 'if' statement", function(){
it("will execute a statement if a condition is true", function(){
var a = "Hello";
if(13 > 9){
a = "Goodbye";
}
assert.strictEqual(___, a);
});
it("can have an 'else' clause that will execute if the condition is false", function(){
var a = "Giraffe";
if(34 > 55){
a = "Elephant";
} else {
a = "Crocodile";
}
assert.strictEqual(___, a);
});
it("can have multiple clauses each depedning on a condition", function(){
var a = "Parrot";
if(1>2){
a = "Horse";
} else if (3 >4) {
a = "Octopus";
} else if (2>1) {
a = "Donkey";
} else {
a = "Lion";
}
assert.strictEqual(___, a);
});
it("can have any boolean expression in its conditional", function(){
var a = "Hippo";
if(13 > 44 || (1 > 0 && 4 > 5)){
a = "Puffer fish";
}
assert.strictEqual(___, a);
});
it("Including function calls", function(){
var a = "Seagull";
if(isNaN(a)){
a = "Albatross";
}
assert.strictEqual(___, a);
});
});
describe("The 'while' statement", function(){
it("will execute code while a condition is true", function(){
var a = 0;
var b = 0;
while(a<5){
a++;
b = (b+2)*2;
}
assert.strictEqual(___, b);
});
it("will never execute if the codnition is false", function(){
var a = "Capuchin";
while(2>3){
a = a + a;
}
assert.strictEqual(___, a);
});
});
describe("The 'do-while' statement", function(){
it("will execute its code at least once", function(){
var a = 192;
do {
a = a + a;
} while (a < 33);
assert.strictEqual(___, a);
});
});
describe("the 'break' special statement", function(){
it("will break out of a loop",function(){
var a = 99;
while(true){// this will loop forever!
a = a - 1;
break;
}
assert.strictEqual(___, a);
});
it("is mostly useful when used with a condition", function(){
var a = 4;
var b = 19;
while(true){
a++;
b = b - 2;
if( a+b == 20){
break;
}
}
assert.strictEqual(___, a);
assert.strictEqual(___, b);
});
});
describe("The 'switch' statement", function(){
it("is like a series of else-if",function(){
var animal = "Carp";
var animal = undefined;
switch(a){
case 'Dog':
sound = "Woof";
break
case 'Carp':
sound = "Glup Glup";
break;
case 'Cat':
sound = "Meow";
break;
}
assert.strictEqual(___, sound);
});
it("can have a default case", function(){
var where_i_went_on_holiday = "HD 188753";
var reaction = undefined;
switch(where_i_went_on_holiday){
case "Seaside":
reaction = "How lovely :)";
break;
case "The mountain":
reaction = "I love the snow!";
break;
case "The country":
reaction = "Why of course dear.";
break;
default:
reaction = "Whoa, trippy!";
break;
}
assert.strictEqual(___, reaction);
});
it("will continue executing unless it encounters a break", function(){
var creature = "Orc";
var braids_hair = undefined;
var smells_bad = undefined;
var eats_goat = undefined;
switch(creature){
case "Orc":
braids_hair = true;
case "Human":
eats_goat = true;
case "Dwarf":
smells_bad = true;
break;
case "Elf":
braids_hair = true;
eats_goat = false;
smells_bad = false;
break;
}
assert(eats_goat === ___);
assert(braids_hair === ___);
assert(smells_bad === ___);
});
});
});