Skip to content

Commit 73c6efb

Browse files
committed
Added bugs 9 - 12
1 parent 379a9a9 commit 73c6efb

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

fundamentals/bug-challenge-es6/__tests__/bug-challenge-tests.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,55 @@ describe('Bug challenge ES6', () => {
130130

131131
});
132132

133+
describe('bug9', () => {
134+
135+
it("should list only BMWs", () => {
136+
challenge.bug9();
137+
138+
expect(console.logs).toEqual([
139+
'BMW i8',
140+
'BMW M3'
141+
]);
142+
});
143+
144+
});
145+
146+
describe('bug10', () => {
147+
148+
it("should print 'Help'", () => {
149+
challenge.bug10();
150+
151+
expect(console.logs).toEqual([
152+
'Help'
153+
]);
154+
});
155+
156+
});
157+
158+
describe('bug11', () => {
159+
160+
it("should correctly add players Alice & Bob", () => {
161+
challenge.bug11();
162+
163+
expect(console.logs).toEqual([
164+
'Player Alice has 0 points',
165+
'Player Bob has 0 points'
166+
]);
167+
});
168+
169+
});
170+
171+
describe('bug12', () => {
172+
173+
it("should not change the value of the outer y", () => {
174+
challenge.bug12();
175+
176+
expect(console.logs).toEqual([
177+
'Printing vector at (6, 7)',
178+
'y=5'
179+
]);
180+
});
181+
182+
});
183+
133184
});

fundamentals/bug-challenge-es6/bug-challenge.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,77 @@ export default class BugChallenge {
132132
}
133133
}
134134

135+
bug9() {
136+
const cars = [{
137+
make: 'Volvo',
138+
type: 'S90'
139+
}, {
140+
make: 'BMW',
141+
type: 'i8'
142+
}, {
143+
make: 'BMW',
144+
type: 'M3'
145+
}, {
146+
make: 'Audi',
147+
type: 'A6'
148+
}];
149+
150+
function findCars(make) {
151+
return cars.filter(car => car.make = make);
152+
}
153+
154+
for (let bmw of findCars('BMW')) {
155+
console.log(`${bmw.make} ${bmw.type}`);
156+
}
157+
}
158+
159+
bug10() {
160+
const command = 'printHelp';
161+
162+
switch (command) {
163+
case 'printMath':
164+
console.log(`√9=${Math.sqrt(9)}`);
165+
case 'printHelp':
166+
console.log('Help');
167+
case 'quit':
168+
console.log('Quitting');
169+
}
170+
}
171+
172+
bug11() {
173+
class Game {
174+
constructor() {
175+
this.players = [];
176+
}
177+
178+
addPlayers(names) {
179+
names.forEach(function (name) {
180+
this.players.push({name, points: 0});
181+
});
182+
}
183+
}
184+
185+
const game = new Game();
186+
game.addPlayers(['Alice', 'Bob']);
187+
188+
for (let player of game.players) {
189+
console.log(`Player ${player.name} has ${player.points} points`);
190+
}
191+
}
192+
193+
bug12() {
194+
let y = 5;
195+
196+
function printVector() {
197+
let x = 6;
198+
y = 7;
199+
200+
console.log(`Printing vector at (${x}, ${y})`);
201+
}
202+
203+
printVector();
204+
console.log(`y=${y}`);
205+
}
206+
207+
135208
}

0 commit comments

Comments
 (0)