From 4e9f7a7ef73f3ac87ec12ab123dda503aded545b Mon Sep 17 00:00:00 2001 From: Tarek Tarho Date: Sun, 27 Nov 2016 15:58:57 +0100 Subject: [PATCH 1/3] Added text file --- fundamentals/bug-challenge-es6/test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 fundamentals/bug-challenge-es6/test.txt diff --git a/fundamentals/bug-challenge-es6/test.txt b/fundamentals/bug-challenge-es6/test.txt new file mode 100644 index 000000000..dab621ca7 --- /dev/null +++ b/fundamentals/bug-challenge-es6/test.txt @@ -0,0 +1 @@ +sdfsdf From 9a26a7be4254373a973d6410fac1c56751ace6f4 Mon Sep 17 00:00:00 2001 From: Tarek Tarho Date: Sun, 27 Nov 2016 16:00:16 +0100 Subject: [PATCH 2/3] Added text --- fundamentals/bug-challenge-es6/test.txt | 1 - fundamentals/bug-challenge-es6/test.txt.txt | 0 2 files changed, 1 deletion(-) delete mode 100644 fundamentals/bug-challenge-es6/test.txt create mode 100644 fundamentals/bug-challenge-es6/test.txt.txt diff --git a/fundamentals/bug-challenge-es6/test.txt b/fundamentals/bug-challenge-es6/test.txt deleted file mode 100644 index dab621ca7..000000000 --- a/fundamentals/bug-challenge-es6/test.txt +++ /dev/null @@ -1 +0,0 @@ -sdfsdf diff --git a/fundamentals/bug-challenge-es6/test.txt.txt b/fundamentals/bug-challenge-es6/test.txt.txt new file mode 100644 index 000000000..e69de29bb From f710ee4bc441dca670f7701082e3ad93d7f6ade5 Mon Sep 17 00:00:00 2001 From: tarek tarho Date: Fri, 2 Dec 2016 23:44:56 +0100 Subject: [PATCH 3/3] 'g' --- .../__tests__/bug-challenge-tests.js | 103 ++++++++- .../bug-challenge-es6/bug-challenge.js | 212 ++++++++++++++---- fundamentals/bug-challenge-es6/package.json | 3 + 3 files changed, 271 insertions(+), 47 deletions(-) diff --git a/fundamentals/bug-challenge-es6/__tests__/bug-challenge-tests.js b/fundamentals/bug-challenge-es6/__tests__/bug-challenge-tests.js index c6eba3cf1..e72808093 100644 --- a/fundamentals/bug-challenge-es6/__tests__/bug-challenge-tests.js +++ b/fundamentals/bug-challenge-es6/__tests__/bug-challenge-tests.js @@ -1,12 +1,12 @@ import BugChallenge from '../bug-challenge'; import '../jest-helpers'; -const challenge = new BugChallenge(); - describe('Bug challenge ES6', () => { + let challenge; beforeEach(() => { console.clear(); + challenge = new BugChallenge(); }); describe('bug1', () => { @@ -102,7 +102,7 @@ describe('Bug challenge ES6', () => { describe('bug7', () => { it("should first run with stopOnError=all and then with stopOnError=null", () => { - challenge.bug6(); + challenge.bug7(); expect(console.logs).toEqual([ 'run: stopOnError=all', @@ -130,4 +130,99 @@ describe('Bug challenge ES6', () => { }); -}); \ No newline at end of file + describe('bug9', () => { + + it("should list only BMWs", () => { + challenge.bug9(); + + expect(console.logs).toEqual([ + 'BMW i8', + 'BMW M3' + ]); + }); + + }); + + describe('bug10', () => { + + it("should print 'Help'", () => { + challenge.bug10(); + + expect(console.logs).toEqual([ + 'Help' + ]); + }); + + }); + + describe('bug11', () => { + + it("should correctly add players Alice & Bob", () => { + challenge.bug11(); + + expect(console.logs).toEqual([ + 'Player Alice has 0 points', + 'Player Bob has 0 points' + ]); + }); + + }); + + describe('bug12', () => { + + it("should not change the value of the outer y", () => { + challenge.bug12(); + + expect(console.logs).toEqual([ + 'Printing vector at (6, 7)', + 'y=5' + ]); + }); + + }); + describe('bug13', () => { + it("should return that AI, Godfather and Inception are in the top10 movie list", () => { + challenge.bug13(); + jest.runAllTimers(); + + expect(console.logs).toEqual([ + "Independence Day is not in the top 10!", + "AI is in the top 10!", + "Godfather is in the top 10!", + "Inception is in the top 10!" + ]); + }); + + }); + describe('bug14', () => { + it("should return that AI is best movie ever", () => { + challenge.bug14(); + jest.runAllTimers(); + + expect(console.logs).toEqual([ + "AI is best movie ever", + "Godfather is not best movie ever", + ]); + }); + }); + describe('bug15', () => { + it("should return Al Pacino as first actor after sorting alphabetically", () => { + challenge.bug15(); + jest.runAllTimers(); + + expect(console.logs).toEqual([ + 'The first actor when sorted alphabetically is Al Pacino' + ]); + }); + }); + describe('bug16', () => { + it("should return that Al Pacino is ranked 4th among all actors", () => { + challenge.bug16(); + jest.runAllTimers(); + + expect(console.logs).toEqual([ + 'Al Pacino is ranked 4' + ]); + }); + }); +}); diff --git a/fundamentals/bug-challenge-es6/bug-challenge.js b/fundamentals/bug-challenge-es6/bug-challenge.js index 574e1bbe3..6a6ff84ea 100644 --- a/fundamentals/bug-challenge-es6/bug-challenge.js +++ b/fundamentals/bug-challenge-es6/bug-challenge.js @@ -1,5 +1,31 @@ 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 @@ -15,18 +41,21 @@ export default class BugChallenge { age: 40 }]; - for (let person in people) { + for (let person of people) { console.log(`${person.name} is ${person.age}`); } } + //bug1 i changed the for loop from let preson in people to let preson of people bug2() { const array = [1, 2, 3, 4]; + array.reverse(); - for (let i = 0; i < array.length; i++) { - console.log(array.pop()); + for (let array of array) { + console.log(array); } } + //bug2 i used the fuction .reverse to revers the content of the arrray then i used then i chend the for loop bug3() { const array = {}; @@ -35,55 +64,36 @@ export default class BugChallenge { array[2] = 'c'; let total = 0; - for (let key in obj) { + + + + for (let key in array) { total += key; } - console.log(total); + console.log(total.length -1); } + //bug 3 i changed the concole log to total.length -1 and + // and key in array bug4() { - const top10Movies = [ - 'AI', - 'Shawshank Redemption', - 'Godfather', - 'Pulp Fiction', - 'Fight club', - 'Forrest Gump', - 'Inception', - 'Goodfellas', - 'The Matrix', - 'Interstellar' - ] - const top10Actors = [ - 'Marlon Brando', - 'Jack Nickolson', - 'Robert De Niro', - 'Al Pacino', - 'Daniel Day-Lewis', - 'Duston Hoffman', - 'Tom Hanks', - 'Anthony Hopkins', - 'Paul Newman', - 'Denzel Washington' - - ] - // We list all movies, except the top 3. - var index = 3; - for (index; index < top10Movies.length; index++) { - console.log(`movie: ${top10Movies[index]}`); + let index = 3; + for (index; index < this.top10Movies.length; index++) { + console.log(`movie: ${this.top10Movies[index]}`); } + index = 3; // We also list all actors, except the top 3. - for (index; index < top10Actors.length; index++) { - console.log(`actor: ${top10Actors[index]}`); + for (index; index < this.top10Actors.length; index++) { + console.log(`actor: ${this.top10Actors[index]}`); } } + //bug4 i chenged the var to let and after the first loop i set index = 3 agine bug5() { const defaultMethod = 'GET'; - const defaultUseCaching = true; + const defaultUseCaching = false; function fetch(options) { const url = options.url; @@ -99,21 +109,24 @@ export default class BugChallenge { }); } - bug6() { + bug6() { function run(options) { if (options.script == undefined) { options.script = 'main.js'; } - + console.log(`run: script=${options.script}`); } + run({}); - run(); + } + //bug6 i set options to undefined useing this {} + bug7() { function run(options = {}) { - if (options.stopOnError == undefined) { + if (options.stopOnError === undefined) { options.stopOnError = 'all'; } @@ -123,13 +136,126 @@ export default class BugChallenge { run(); run({stopOnError: null}); } + //bug7 not working!! bug8() { - for (var i = 0; i < 5; i++) { - setTimeout(function () { + for (let i = 0; i < 5; i++) { + setTimeout( () => { console.log(i+1); }, 100*i); } } + //bug8 i chenged the var to let and function to => + + 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}`); + } + } + //bug9 i have add == to the return :) + + bug10() { + const command = 'printHelp'; + + switch (command) { + case 'printMath': + console.log(`√9=${Math.sqrt(9)}`); + case 'printHelp': + console.log('Help'); + break; + case 'quit': + console.log('Quitting'); + } + } + //bug10 i have add 1 break after console.log help + + bug11() { + class Game { + constructor() { + this.players = []; + } + + addPlayers(names) { + names.forEach( (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`); + } + } + //bug 11 changed the function in names.forEach line to => + + bug12() { + let y = 5; + + function printVector() { + let x = 6; + let y = 7; + + console.log(`Printing vector at (${x}, ${y})`); + } + + printVector(); + console.log(`y=${y}`); + } + //bug12 add let to y + + bug13() { + let notInTop10 = (movieName) => { + return this.top10Movies.indexOf(movieName)== -1 + } + 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!'); + } + //bug13 changed the var to let and removed the ! and add == -1 *****(i have to do it agine i did't understandet well********) + bug14() { + + let isInFirstPlace = (movieName) => { + return this.top10Movies[0] === movieName + } + console.log('AI is ' + (isInFirstPlace('AI')?'':'not ') + 'best movie ever') + console.log('Godfather is ' + (isInFirstPlace('Godfather')?'':'not ') + 'best movie ever') + } + //bug14 changed console log place from the top to the botom + bug15() { + let getAlphabeticalFirst = ()=> { + return this.top10Actors.sort()[0] + } + + console.log(`The first actor when sorted alphabetically is ${getAlphabeticalFirst()}`) + } + //bug14 changed the function to => and var to let + bug16() { + const ranking = this.top10Actors.indexOf('Al Pacino'); + // var thirdRankedActor = this.top10Actors['2']; + console.log(`Al Pacino is ranked ${ranking + 1}`) + } } +//bug16 the nummber's can't be string :P diff --git a/fundamentals/bug-challenge-es6/package.json b/fundamentals/bug-challenge-es6/package.json index 2c2805551..6f5562561 100644 --- a/fundamentals/bug-challenge-es6/package.json +++ b/fundamentals/bug-challenge-es6/package.json @@ -6,6 +6,9 @@ "/node_modules/" ] }, + "scripts":{ + "test": "jest" + }, "devDependencies": { "babel-core": "^6.18.2", "babel-jest": "^17.0.2",