Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 79e8336

Browse files
author
hruy28
committed
Changes to the codes based on joost suggestions
1 parent 009aa0d commit 79e8336

2 files changed

Lines changed: 37 additions & 41 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import BugChallenge from '../bug-challenge';
22
import '../jest-helpers';
33

4-
const challenge = new BugChallenge();
5-
64
describe('Bug challenge ES6', () => {
75

6+
let challenge;
87
beforeEach(() => {
98
console.clear();
9+
challenge = new BugChallenge();
1010
});
1111

12+
1213
describe('bug1', () => {
1314

1415
it("should list the names and ages of people", () => {

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,44 +50,40 @@ export default class BugChallenge {
5050
bug2() {
5151
const array = [1, 2, 3, 4];
5252

53-
for (let i = 0; i < array.length; i++) {
54-
console.log(array.pop());
55-
console.log(array.pop());
56-
console.log(array.pop());
57-
console.log(array.pop());
58-
59-
}
53+
while (array.length > 0) {
54+
console.log(array.pop());
55+
}
6056
}
6157
/* bug2: I used console log and the functions pop() so that every time the function loops
6258
it prints out the last element in the array. So that it is reversed.*/
6359

6460
bug3() {
6561
const array = [];
66-
array[0] = 0;
67-
array[1] = 1;
68-
array[2] = 2;
69-
62+
array[0] = 'a';
63+
array[1] = 'b';
64+
array[2] = 'c';
65+
66+
7067
let total = 0;
71-
for (let key of array) {
72-
total += key;
68+
for (let key in array) {
69+
total = ++key;
7370
}
7471

7572
console.log(total);
7673
}
7774
/* bug3: First I replace the empty curly bracket with an empty square bracket as it is an array.
78-
Second I replace the string values with number values so that it can perform addition. Third
79-
I replace the keyword 'in' with 'of'. */
75+
Then I increment the index number of the array and assigned it to the variable total. */
8076

8177
bug4() {
8278
// We list all movies, except the top 3.
83-
var index = 3;
84-
for (index; index < this.top10Movies.length; index++) {
79+
80+
for (let index = 3; index < this.top10Movies.length; index++) {
8581
console.log(`movie: ${this.top10Movies[index]}`);
8682
}
8783

8884
// We also list all actors, except the top 3.
89-
var index = 3;
90-
for (index; index < this.top10Actors.length; index++) {
85+
86+
for (let index = 3; index < this.top10Actors.length; index++) {
9187
console.log(`actor: ${this.top10Actors[index]}`);
9288
}
9389
}
@@ -96,12 +92,12 @@ export default class BugChallenge {
9692

9793
bug5() {
9894
const defaultMethod = 'GET';
99-
const defaultUseCaching = false;
95+
const defaultUseCaching = true;
10096

10197
function fetch(options) {
10298
const url = options.url;
10399
const method = options.method || defaultMethod;
104-
const useCaching = options.useCaching || defaultUseCaching;
100+
const useCaching = options.useCaching;
105101

106102
console.log(`fetch: ${method} ${url} (useCaching=${useCaching})`);
107103
}
@@ -111,7 +107,10 @@ export default class BugChallenge {
111107
useCaching: false
112108
});
113109
}
114-
// bug5: I changed the default value for caching to false as the test was to disable caching for fetch.
110+
111+
/* bug5: I deleted the default value for caching on the const 'useCaching'. Although I need more explanation
112+
on this. And I hope I have done the right thing here. */
113+
115114

116115
bug6() {
117116
function run(options = {}) {
@@ -141,19 +140,15 @@ export default class BugChallenge {
141140
/* bug7: The comparison operator inside the if statement must compare for the exact value and
142141
type '===' inorder to run the second callback*/
143142

144-
bug8() {
145-
for (var i = 1; i <= 5; i++) {
146-
setDelay(i);
147-
}
148-
function setDelay(i) {
149-
setTimeout(function () {
150-
console.log(i);
151-
}, 100);
152-
153-
}
143+
bug8() {
144+
for (let i = 0; i < 5; i++) {
145+
setTimeout(() => {
146+
console.log(i+1);
147+
}, 100*i);
148+
}
154149
}
155-
/* bug8: It set the timeout from within a function as the previous code has drawbacks. It printed the
156-
number 6 five times as the variables passed don't keep their intial value.*/
150+
/* bug8: I replaced 'var' with the keyword 'let' so that it can be used only with function. And I
151+
also replaced the anonymous function with the arrow function.*/
157152

158153
bug9() {
159154
const cars = [{
@@ -206,9 +201,9 @@ export default class BugChallenge {
206201
}
207202

208203
addPlayers(names) {
209-
names.forEach(function (name) {
204+
names.forEach((name) => {
210205
this.players.push({name, points: 0});
211-
}, this);
206+
});
212207
}
213208
}
214209

@@ -258,8 +253,8 @@ export default class BugChallenge {
258253
console.log('Godfather is ' + (isInFirstPlace('Godfather')?'':'not ') + 'best movie ever');
259254

260255
}
261-
/* bug14: I put the console log within the function so as it can print after the function is executed.
262-
Before it was returnig error message as it was outside the function.*/
256+
/* bug14:Putting the console.log statements after the function declaration solved the problem. That is
257+
because functions are hoisted up, and variables are not (only their declaration is).*/
263258

264259
bug15() {
265260
var getAlphabeticalFirst = () => {
@@ -273,7 +268,7 @@ export default class BugChallenge {
273268

274269
bug16() {
275270
const ranking = this.top10Actors.indexOf('Al Pacino');
276-
console.log(`Al Pacino is ranked ${ranking + 4}`)
271+
console.log(`Al Pacino is ranked ${ranking + 1}`)
277272
}
278273

279274
/* bug16: Before the number 1 was within a string so it was concatenated. The result was '31'.

0 commit comments

Comments
 (0)