From e188134c6c5d5758c631b551beff59f5b238072e Mon Sep 17 00:00:00 2001 From: Maxence Bouret Date: Fri, 22 Mar 2019 12:50:47 +0000 Subject: [PATCH 1/3] Update viking.js --- starter-code/src/viking.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/starter-code/src/viking.js b/starter-code/src/viking.js index 46a229138..9017bfc8a 100755 --- a/starter-code/src/viking.js +++ b/starter-code/src/viking.js @@ -1,11 +1,11 @@ // Soldier -function Soldier() {} +class Soldier {} // Viking -function Viking() {} +class Viking {} // Saxon -function Saxon() {} +class Saxon {} // War -function War() {} +class War {} From e739b106f828b933774f1916c8d61ffe0c55aa8d Mon Sep 17 00:00:00 2001 From: Maxence Bouret Date: Fri, 7 Jun 2019 12:33:32 +0100 Subject: [PATCH 2/3] Update readme.md --- readme.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index c9093f3b8..fa48894ff 100755 --- a/readme.md +++ b/readme.md @@ -62,32 +62,34 @@ Now we have to write the correct code in the `src/viking.js` file to make the te ```javascript // Soldier -function Soldier () {} +class Soldier {} // Viking -function Viking () {} +class Viking {} // Saxon -function Saxon () {} +class Saxon {} // War -function War () {} +class War {} ``` In this case, the test says that _Soldier constructor function should receive 2 arguments (health & strength)_, so we have to write the correct code that passes this test. Let's make the `Soldier` constructor function receive two arguments: ```javascript // Soldier -function Soldier (healthArg, strengthArg) {} +class Soldier { + constructor(healthArg, strengthArg) {} +} // Viking -function Viking () {} +class Viking {} // Saxon -function Saxon () {} +class Saxon {} // War -function War () {} +class War {} ``` **Execute all the tests** From cdd28d1ea45ce0cef808efa7b32afda384789510 Mon Sep 17 00:00:00 2001 From: Sergio Cordero Date: Fri, 28 Jun 2019 23:34:06 +0200 Subject: [PATCH 3/3] OOP Exercise completed --- starter-code/src/viking.js | 88 +++++- starter-code/tests/VikingSpec.js | 480 +++++++++++++++---------------- 2 files changed, 324 insertions(+), 244 deletions(-) diff --git a/starter-code/src/viking.js b/starter-code/src/viking.js index 9017bfc8a..7f99b10b0 100755 --- a/starter-code/src/viking.js +++ b/starter-code/src/viking.js @@ -1,11 +1,91 @@ // Soldier -class Soldier {} +function Soldier(health, strength) { + this.health = health; + this.strength = strength; +} + +Soldier.prototype.attack = function() { + return this.strength; +} +Soldier.prototype.receiveDamage = function(damage) { + this.health = this.health - damage; +} // Viking -class Viking {} +function Viking(name, health, strength) { + Soldier.call(this, health, strength); + this.name = name; +} + +Viking.prototype = Object.create(Soldier.prototype); +Viking.prototype.constructor = Viking; + +Viking.prototype.battleCry = function() { + return "Odin Owns You All!"; +} + +Viking.prototype.receiveDamage = function(damage) { + this.health = this.health - damage; + var res = (this.health <= 0) ? `${this.name} has died in act of combat` : `${this.name} has received ${damage} points of damage`; + return res; +} // Saxon -class Saxon {} +function Saxon(health, strength) { + Soldier.call(this, health, strength); +} + +Saxon.prototype = Object.create(Soldier.prototype); +Saxon.prototype.constructor = Saxon; + +Saxon.prototype.receiveDamage = function(damage) { + this.health = this.health - damage; + var res = (this.health <= 0) ? `A Saxon has died in combat` : `A Saxon has received ${damage} points of damage`; + return res; +} // War -class War {} +function War() { + this.vikingArmy = []; + this.saxonArmy = []; +} + +War.prototype.addViking = function(Viking) { + this.vikingArmy.push(Viking); +} + +War.prototype.addSaxon = function(Saxon) { + this.saxonArmy.push(Saxon); +}; + +War.prototype.vikingAttack = function() { + var saxon = this.saxonArmy[Math.floor(Math.random()*this.saxonArmy.length)]; + var viking = this.vikingArmy[Math.floor(Math.random()*this.vikingArmy.length)]; + var attackResult = saxon.receiveDamage(viking.strength); + if(attackResult === 'A Saxon has died in combat') { + this.saxonArmy.splice(this.saxonArmy.indexOf(saxon), 1); + } + return attackResult; +}; + +War.prototype.saxonAttack = function() { + var saxon = this.saxonArmy[Math.floor(Math.random()*this.saxonArmy.length)]; + var viking = this.vikingArmy[Math.floor(Math.random()*this.vikingArmy.length)]; + var attackResult = viking.receiveDamage(saxon.strength); + if(attackResult === viking.name + " has died in act of combat") { + this.vikingArmy.splice(this.vikingArmy.indexOf(viking), 1); + } + return attackResult; +}; + +War.prototype.showStatus = function() { + var res = ""; + if(this.saxonArmy.length === 0) { + res = "Vikings have won the war of the century!"; + }else if(this.vikingArmy.length === 0) { + res = "Saxons have fought for their lives and survive another day..."; + }else{ + res = "Vikings and Saxons are still in the thick of battle."; + } + return res; +}; diff --git a/starter-code/tests/VikingSpec.js b/starter-code/tests/VikingSpec.js index ebaea7cfa..8dcd9bce8 100755 --- a/starter-code/tests/VikingSpec.js +++ b/starter-code/tests/VikingSpec.js @@ -8,50 +8,50 @@ describe("Soldier", function () { }); describe("constructor function", function () { - // it("should receive 2 arguments (health & strength)", function () { - // expect(Soldier.length).toEqual(2); - // }); + it("should receive 2 arguments (health & strength)", function () { + expect(Soldier.length).toEqual(2); + }); - // it("should receive the health property as its 1st argument", function () { - // expect(soldier.health).toEqual(health); - // }); + it("should receive the health property as its 1st argument", function () { + expect(soldier.health).toEqual(health); + }); - // it("should receive the strength property as its 2nd argument", function () { - // expect(soldier.strength).toEqual(strength); - // }); + it("should receive the strength property as its 2nd argument", function () { + expect(soldier.strength).toEqual(strength); + }); }); describe("attack() method", function () { - // it("should be a function", function () { - // expect(typeof(soldier.attack)).toBe("function"); - // }); + it("should be a function", function () { + expect(typeof(soldier.attack)).toBe("function"); + }); - // it("should receive 0 arguments", function () { - // expect(soldier.attack.length).toEqual(0); - // }); + it("should receive 0 arguments", function () { + expect(soldier.attack.length).toEqual(0); + }); - // it("should return the strength property of the Soldier", function () { - // expect(soldier.attack()).toEqual(strength); - // }); + it("should return the strength property of the Soldier", function () { + expect(soldier.attack()).toEqual(strength); + }); }); describe("receiveDamage() method", function () { - // it("should be a function", function () { - // expect(typeof(soldier.receiveDamage)).toBe("function"); - // }); - - // it("should receive 1 argument (the damage)", function () { - // expect(soldier.receiveDamage.length).toEqual(1); - // }); - - // it("should remove the received damage from the health property", function () { - // soldier.receiveDamage(50); - // expect(soldier.health).toEqual(health - 50); - // }); - - // it("shouldn't return anything", function () { - // expect(soldier.receiveDamage(50)).toEqual(undefined); - // }); + it("should be a function", function () { + expect(typeof(soldier.receiveDamage)).toBe("function"); + }); + + it("should receive 1 argument (the damage)", function () { + expect(soldier.receiveDamage.length).toEqual(1); + }); + + it("should remove the received damage from the health property", function () { + soldier.receiveDamage(50); + expect(soldier.health).toEqual(health - 50); + }); + + it("shouldn't return anything", function () { + expect(soldier.receiveDamage(50)).toEqual(undefined); + }); }); }); @@ -66,78 +66,78 @@ describe("Viking", function () { viking = new Viking(name, health, strength); }); - // it("should inherit from Soldier", function () { - // expect(viking instanceof Soldier).toEqual(true); - // }); + it("should inherit from Soldier", function () { + expect(viking instanceof Soldier).toEqual(true); + }); describe("constructor function", function () { - // it("should receive 3 arguments (name, health & strength)", function () { - // expect(Viking.length).toEqual(3); - // }); + it("should receive 3 arguments (name, health & strength)", function () { + expect(Viking.length).toEqual(3); + }); - // it("should receive the name property as its 1st argument", function () { - // expect(viking.name).toEqual(name); - // }); + it("should receive the name property as its 1st argument", function () { + expect(viking.name).toEqual(name); + }); - // it("should receive the health property as its 2nd argument", function () { - // expect(viking.health).toEqual(health); - // }); + it("should receive the health property as its 2nd argument", function () { + expect(viking.health).toEqual(health); + }); - // it("should receive the strength property as its 3rd argument", function () { - // expect(viking.strength).toEqual(strength); - // }); + it("should receive the strength property as its 3rd argument", function () { + expect(viking.strength).toEqual(strength); + }); }); describe("attack() method", function () { - // it("should be a function", function () { - // expect(typeof(viking.attack)).toBe("function"); - // }); + it("should be a function", function () { + expect(typeof(viking.attack)).toBe("function"); + }); - // it("should receive 0 arguments", function () { - // expect(viking.attack.length).toEqual(0); - // }); + it("should receive 0 arguments", function () { + expect(viking.attack.length).toEqual(0); + }); - // it("should return the strength property of the Viking", function () { - // expect(viking.attack()).toEqual(strength); - // }); + it("should return the strength property of the Viking", function () { + expect(viking.attack()).toEqual(strength); + }); }); describe("receiveDamage() method", function () { - // it("should be a function", function () { - // expect(typeof(viking.receiveDamage)).toBe("function"); - // }); - - // it("should receive 1 argument (the damage)", function () { - // expect(viking.receiveDamage.length).toEqual(1); - // }); - - // it("should remove the received damage from the health property", function () { - // viking.receiveDamage(50); - // expect(viking.health).toEqual(health - 50); - // }); - - // it("should return \"NAME has received DAMAGE points of damage\", if the Viking is still alive", function () { - // expect(viking.receiveDamage(50)).toEqual(name + " has received 50 points of damage"); - // expect(viking.receiveDamage(75)).toEqual(name + " has received 75 points of damage"); - // }); - - // it("should return \"NAME has died in act of combat\", if the Viking dies", function () { - // expect(viking.receiveDamage(health)).toEqual(name + " has died in act of combat"); - // }); + it("should be a function", function () { + expect(typeof(viking.receiveDamage)).toBe("function"); + }); + + it("should receive 1 argument (the damage)", function () { + expect(viking.receiveDamage.length).toEqual(1); + }); + + it("should remove the received damage from the health property", function () { + viking.receiveDamage(50); + expect(viking.health).toEqual(health - 50); + }); + + it("should return \"NAME has received DAMAGE points of damage\", if the Viking is still alive", function () { + expect(viking.receiveDamage(50)).toEqual(name + " has received 50 points of damage"); + expect(viking.receiveDamage(75)).toEqual(name + " has received 75 points of damage"); + }); + + it("should return \"NAME has died in act of combat\", if the Viking dies", function () { + expect(viking.receiveDamage(health)).toEqual(name + " has died in act of combat"); + }); }); describe("battleCry() method", function () { - // it("should be a function", function () { - // expect(typeof(viking.battleCry)).toBe("function"); - // }); + it("should be a function", function () { + expect(typeof(viking.battleCry)).toBe("function"); + }); - // it("should receive 0 arguments", function () { - // expect(viking.battleCry.length).toEqual(0); - // }); + it("should receive 0 arguments", function () { + expect(viking.battleCry.length).toEqual(0); + }); - // it("should return \"Odin Owns You All!\"", function () { - // expect(viking.battleCry()).toEqual("Odin Owns You All!"); - // }); + it("should return \"Odin Owns You All!\"", function () { + expect(viking.battleCry()).toEqual("Odin Owns You All!"); + }); }); }); @@ -151,60 +151,60 @@ describe("Saxon", function () { saxon = new Saxon(health, strength); }); - // it("should inherit from Soldier", function () { - // expect(saxon instanceof Soldier).toEqual(true); - // }); + it("should inherit from Soldier", function () { + expect(saxon instanceof Soldier).toEqual(true); + }); describe("constructor function", function () { - // it("should receive 2 arguments (health & strength)", function () { - // expect(Saxon.length).toEqual(2); - // }); + it("should receive 2 arguments (health & strength)", function () { + expect(Saxon.length).toEqual(2); + }); - // it("should receive the health property as its 1st argument", function () { - // expect(saxon.health).toEqual(health); - // }); + it("should receive the health property as its 1st argument", function () { + expect(saxon.health).toEqual(health); + }); - // it("should receive the strength property as its 2nd argument", function () { - // expect(saxon.strength).toEqual(strength); - // }); + it("should receive the strength property as its 2nd argument", function () { + expect(saxon.strength).toEqual(strength); + }); }); describe("attack() method", function () { - // it("should be a function", function () { - // expect(typeof(saxon.attack)).toBe("function"); - // }); + it("should be a function", function () { + expect(typeof(saxon.attack)).toBe("function"); + }); - // it("should receive 0 arguments", function () { - // expect(saxon.attack.length).toEqual(0); - // }); + it("should receive 0 arguments", function () { + expect(saxon.attack.length).toEqual(0); + }); - // it("should return the strength property of the Saxon", function () { - // expect(saxon.attack()).toEqual(strength); - // }); + it("should return the strength property of the Saxon", function () { + expect(saxon.attack()).toEqual(strength); + }); }); describe("receiveDamage() method", function () { - // it("should be a function", function () { - // expect(typeof(saxon.receiveDamage)).toBe("function"); - // }); - - // it("should receive 1 argument (the damage)", function () { - // expect(saxon.receiveDamage.length).toEqual(1); - // }); - - // it("should remove the received damage from the health property", function () { - // saxon.receiveDamage(50); - // expect(saxon.health).toEqual(health - 50); - // }); - - // it("should return \"A Saxon has received DAMAGE points of damage\", if the Saxon is still alive", function () { - // expect(saxon.receiveDamage(45)).toEqual("A Saxon has received 45 points of damage"); - // expect(saxon.receiveDamage(10)).toEqual("A Saxon has received 10 points of damage"); - // }); - - // it("should return \"A Saxon has died in combat\", if the Saxon dies", function () { - // expect(saxon.receiveDamage(health)).toEqual("A Saxon has died in combat"); - // }); + it("should be a function", function () { + expect(typeof(saxon.receiveDamage)).toBe("function"); + }); + + it("should receive 1 argument (the damage)", function () { + expect(saxon.receiveDamage.length).toEqual(1); + }); + + it("should remove the received damage from the health property", function () { + saxon.receiveDamage(50); + expect(saxon.health).toEqual(health - 50); + }); + + it("should return \"A Saxon has received DAMAGE points of damage\", if the Saxon is still alive", function () { + expect(saxon.receiveDamage(45)).toEqual("A Saxon has received 45 points of damage"); + expect(saxon.receiveDamage(10)).toEqual("A Saxon has received 10 points of damage"); + }); + + it("should return \"A Saxon has died in combat\", if the Saxon dies", function () { + expect(saxon.receiveDamage(health)).toEqual("A Saxon has died in combat"); + }); }); }); @@ -234,55 +234,55 @@ describe("War", function () { }); describe("constructor function", function () { - // it("should receive 0 arguments", function () { - // expect(War.length).toEqual(0); - // }); + it("should receive 0 arguments", function () { + expect(War.length).toEqual(0); + }); - // it("should assign an empty array to the vikingArmy property", function () { - // expect(war.vikingArmy).toEqual([]); - // }); + it("should assign an empty array to the vikingArmy property", function () { + expect(war.vikingArmy).toEqual([]); + }); - // it("should assign an empty array to the saxonArmy property", function () { - // expect(war.saxonArmy).toEqual([]); - // }); + it("should assign an empty array to the saxonArmy property", function () { + expect(war.saxonArmy).toEqual([]); + }); }); describe("addViking() method", function () { - // it("should be a function", function () { - // expect(typeof(war.addViking)).toBe("function"); - // }); - - // it("should receive 1 argument (a Viking object)", function () { - // expect(war.addViking.length).toEqual(1); - // }); - - // it("should add the received Viking to the army", function () { - // war.addViking(viking); - // expect(war.vikingArmy).toEqual([ viking ]); - // }); - - // it("shouldn't return anything", function () { - // expect(war.addViking(viking)).toEqual(undefined); - // }); + it("should be a function", function () { + expect(typeof(war.addViking)).toBe("function"); + }); + + it("should receive 1 argument (a Viking object)", function () { + expect(war.addViking.length).toEqual(1); + }); + + it("should add the received Viking to the army", function () { + war.addViking(viking); + expect(war.vikingArmy).toEqual([ viking ]); + }); + + it("shouldn't return anything", function () { + expect(war.addViking(viking)).toEqual(undefined); + }); }); describe("addSaxon() method", function () { - // it("should be a function", function () { - // expect(typeof(war.addSaxon)).toBe("function"); - // }); - - // it("should receive 1 argument (a Saxon object)", function () { - // expect(war.addSaxon.length).toEqual(1); - // }); - - // it("should add the received Saxon to the army", function () { - // war.addSaxon(saxon); - // expect(war.saxonArmy).toEqual([ saxon ]); - // }); - - // it("shouldn't return anything", function () { - // expect(war.addSaxon(saxon)).toEqual(undefined); - // }); + it("should be a function", function () { + expect(typeof(war.addSaxon)).toBe("function"); + }); + + it("should receive 1 argument (a Saxon object)", function () { + expect(war.addSaxon.length).toEqual(1); + }); + + it("should add the received Saxon to the army", function () { + war.addSaxon(saxon); + expect(war.saxonArmy).toEqual([ saxon ]); + }); + + it("shouldn't return anything", function () { + expect(war.addSaxon(saxon)).toEqual(undefined); + }); }); describe("Armies Attack", function () { @@ -292,81 +292,81 @@ describe("War", function () { }); describe("vikingAttack() method", function () { - // it("should be a function", function () { - // expect(typeof(war.vikingAttack)).toBe("function"); - // }); - - // it("should receive 0 arguments", function () { - // expect(war.vikingAttack.length).toEqual(0); - // }); - - // it("should make Saxon receiveDamage() equal to the strength of a Viking", function () { - // var oldHealth = saxon.health; - // war.vikingAttack(); - // expect(saxon.health).toEqual(oldHealth - viking.strength); - // }); - - // it("should remove dead saxons from the army", function () { - // war.vikingAttack(); - // expect(war.saxonArmy.length).toEqual(0); - // }); - - // it("should return result of calling receiveDamage() of a Saxon with the strength of a Viking", function () { - // expect(war.vikingAttack()).toEqual("A Saxon has died in combat"); - // }); + it("should be a function", function () { + expect(typeof(war.vikingAttack)).toBe("function"); + }); + + it("should receive 0 arguments", function () { + expect(war.vikingAttack.length).toEqual(0); + }); + + it("should make Saxon receiveDamage() equal to the strength of a Viking", function () { + var oldHealth = saxon.health; + war.vikingAttack(); + expect(saxon.health).toEqual(oldHealth - viking.strength); + }); + + it("should remove dead saxons from the army", function () { + war.vikingAttack(); + expect(war.saxonArmy.length).toEqual(0); + }); + + it("should return result of calling receiveDamage() of a Saxon with the strength of a Viking", function () { + expect(war.vikingAttack()).toEqual("A Saxon has died in combat"); + }); }); describe("saxonAttack() method", function () { - // it("should be a function", function () { - // expect(typeof(war.saxonAttack)).toBe("function"); - // }); - - // it("should receive 0 arguments", function () { - // expect(war.saxonAttack.length).toEqual(0); - // }); - - // it("should make a Viking receiveDamage() equal to the strength of a Saxon", function () { - // var oldHealth = viking.health; - // war.saxonAttack(); - // expect(viking.health).toEqual(oldHealth - saxon.strength); - // }); - - // it("should remove dead vikings from the army", function () { - // for (var i = 0; i < 12; i += 1) { - // war.saxonAttack(); - // } - // expect(war.vikingArmy.length).toEqual(0); - // }); - - // it("should return result of calling receiveDamage() of a Viking with the strength of a Saxon", function () { - // expect(war.saxonAttack()).toEqual(viking.name + " has received " + saxon.strength + " points of damage"); - // }); + it("should be a function", function () { + expect(typeof(war.saxonAttack)).toBe("function"); + }); + + it("should receive 0 arguments", function () { + expect(war.saxonAttack.length).toEqual(0); + }); + + it("should make a Viking receiveDamage() equal to the strength of a Saxon", function () { + var oldHealth = viking.health; + war.saxonAttack(); + expect(viking.health).toEqual(oldHealth - saxon.strength); + }); + + it("should remove dead vikings from the army", function () { + for (var i = 0; i < 12; i += 1) { + war.saxonAttack(); + } + expect(war.vikingArmy.length).toEqual(0); + }); + + it("should return result of calling receiveDamage() of a Viking with the strength of a Saxon", function () { + expect(war.saxonAttack()).toEqual(viking.name + " has received " + saxon.strength + " points of damage"); + }); }); describe("showStatus() method", function () { - // it("should be a function", function () { - // expect(typeof(war.showStatus)).toBe("function"); - // }); - - // it("should receive 0 arguments", function () { - // expect(war.showStatus.length).toEqual(0); - // }); - - // it("should return \"Vikings have won the war of the century!\", if the Saxons array is empty", function () { - // war.vikingAttack(); - // expect(war.showStatus()).toEqual("Vikings have won the war of the century!"); - // }); - - // it("should return \"Saxons have fought for their lives and survive another day...\", if the Vikings array is empty", function () { - // for (var i = 0; i < 12; i += 1) { - // war.saxonAttack(); - // } - // expect(war.showStatus()).toEqual("Saxons have fought for their lives and survive another day..."); - // }); - - // it("should return \"Vikings and Saxons are still in the thick of battle.\", if there are still both Vikings and Saxons", function () { - // expect(war.showStatus()).toEqual("Vikings and Saxons are still in the thick of battle."); - // }); + it("should be a function", function () { + expect(typeof(war.showStatus)).toBe("function"); + }); + + it("should receive 0 arguments", function () { + expect(war.showStatus.length).toEqual(0); + }); + + it("should return \"Vikings have won the war of the century!\", if the Saxons array is empty", function () { + war.vikingAttack(); + expect(war.showStatus()).toEqual("Vikings have won the war of the century!"); + }); + + it("should return \"Saxons have fought for their lives and survive another day...\", if the Vikings array is empty", function () { + for (var i = 0; i < 12; i += 1) { + war.saxonAttack(); + } + expect(war.showStatus()).toEqual("Saxons have fought for their lives and survive another day..."); + }); + + it("should return \"Vikings and Saxons are still in the thick of battle.\", if there are still both Vikings and Saxons", function () { + expect(war.showStatus()).toEqual("Vikings and Saxons are still in the thick of battle."); + }); }); }); });