-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbug-challenge.js
More file actions
263 lines (228 loc) · 5.21 KB
/
Copy pathbug-challenge.js
File metadata and controls
263 lines (228 loc) · 5.21 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
export default class BugChallenge {
//Do NOT change the top10Movies and top10Actors variables to fix your tests
//believe me: the problem is in bug() functions, not in these arrays ;)
top10Movies = [
"AI",
"Shawshank Redemption",
"Godfather",
"Pulp Fiction",
"Fight club",
"Forrest Gump",
"Inception",
"Goodfellas",
"The Matrix",
"Interstellar",
];
top10Actors = [
"Marlon Brando",
"Jack Nickolson",
"Robert De Niro",
"Al Pacino",
"Daniel Day-Lewis",
"Duston Hoffman",
"Tom Hanks",
"Anthony Hopkins",
"Paul Newman",
"Denzel Washington",
];
//------
// Bugs
bug1() {
const people = [
{
name: "Alice",
age: 25,
},
{
name: "Bob",
age: 27,
},
{
name: "Charlie",
age: 40,
},
];
for (let person in people) {
console.log(`${person.name} is ${person.age}`);
}
}
bug2() {
const array = [1, 2, 3, 4];
for (let i = 0; i < array.length; i++) {
console.log(array.pop());
}
}
bug3() {
const array = {};
array[0] = "a";
array[1] = "b";
array[2] = "c";
let total = 0;
for (let key in obj) {
total += key;
}
console.log(total);
}
bug4() {
// We list all movies, except the top 3.
var index = 3;
for (index; index < this.top10Movies.length; index++) {
console.log(`movie: ${this.top10Movies[index]}`);
}
// We also list all actors, except the top 3.
for (index; index < top10Actors.length; index++) {
console.log(`actor: ${this.top10Actors[index]}`);
}
}
bug5() {
const defaultMethod = "GET";
const defaultUseCaching = true;
function fetch(options) {
const url = options.url;
const method = options.method || defaultMethod;
const useCaching = options.useCaching || defaultUseCaching;
console.log(`fetch: ${method} ${url} (useCaching=${useCaching})`);
}
fetch({
url: "https://www.example.com",
useCaching: false,
});
}
bug6() {
function run(options) {
if (options.script == undefined) {
options.script = "main.js";
}
console.log(`run: script=${options.script}`);
}
run();
}
bug7() {
function run(options = {}) {
if (options.stopOnError == undefined) {
options.stopOnError = "all";
}
console.log(`run: stopOnError=${options.stopOnError}`);
}
run();
run({ stopOnError: null });
}
bug8() {
for (var i = 0; i < 5; i++) {
setTimeout(function () {
console.log(i + 1);
}, 100 * i);
}
}
bug9() {
const cars = [
{
make: "Volvo",
type: "S90",
},
{
make: "BMW",
type: "i8",
},
{
make: "BMW",
type: "M3",
},
{
make: "Audi",
type: "A6",
},
];
function findCars(make) {
return cars.filter((car) => (car.make = make));
}
for (let bmw of findCars("BMW")) {
console.log(`${bmw.make} ${bmw.type}`);
}
}
bug10() {
const command = "printHelp";
switch (command) {
case "printMath":
console.log(`√9=${Math.sqrt(9)}`);
case "printHelp":
console.log("Help");
case "quit":
console.log("Quitting");
}
}
bug11() {
class Game {
constructor() {
this.players = [];
}
addPlayers(names) {
names.forEach(function (name) {
this.players.push({ name, points: 0 });
});
}
}
const game = new Game();
game.addPlayers(["Alice", "Bob"]);
for (let player of game.players) {
console.log(`Player ${player.name} has ${player.points} points`);
}
}
bug12() {
let y = 5;
function printVector() {
let x = 6;
y = 7;
console.log(`Printing vector at (${x}, ${y})`);
}
printVector();
console.log(`y=${y}`);
}
bug13() {
var notInTop10 = (movieName) => {
return !this.top10Movies.indexOf(movieName);
};
console.log(
"Independence Day is " +
(notInTop10("Independence Day") ? "not " : "") +
"in the top 10!",
);
console.log("AI is " + (notInTop10("AI") ? "not " : "") + "in the top 10!");
console.log(
"Godfather is " +
(notInTop10("Godfather") ? "not " : "") +
"in the top 10!",
);
console.log(
"Inception is " +
(notInTop10("Inception") ? "not " : "") +
"in the top 10!",
);
}
bug14() {
console.log(
"AI is " + (isInFirstPlace("AI") ? "" : "not ") + "best movie ever",
);
console.log(
"Godfather is " +
(isInFirstPlace("Godfather") ? "" : "not ") +
"best movie ever",
);
var isInFirstPlace = (movieName) => {
return this.top10Movies[0] === movieName;
};
}
bug15() {
var getAlphabeticalFirst = function () {
return this.top10Actors.sort()[0];
};
console.log(
`The first actor when sorted alphabetically is ${getAlphabeticalFirst()}`,
);
}
bug16() {
const ranking = this.top10Actors.indexOf("Al Pacino");
// var thirdRankedActor = this.top10Actors['2'];
console.log(`Al Pacino is ranked ${ranking + "1"}`);
}
}