From cf43183dd39be896c6245d419c9c210765191425 Mon Sep 17 00:00:00 2001 From: clenclen Date: Thu, 18 Feb 2016 12:24:10 -0500 Subject: [PATCH 1/3] V1 added superhero and a sound method --- js/super_hero.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/js/super_hero.js b/js/super_hero.js index a68037d..a6f8817 100644 --- a/js/super_hero.js +++ b/js/super_hero.js @@ -1 +1,25 @@ -// var SuperHero = ... + +var SuperHero = function(name, power) { + this.name = name; + this.power = power; + this.getInfo = function(){ + return this.name + " has " + this.power; + } +} + +SuperHero.prototype.sound = "Bang"; +SuperHero.prototype.makeSound = function(){ + return " and says " + this.sound; +} + + +var batman = new SuperHero("Batman", "batarang"); +var superman = new SuperHero("Superman", "super strength") + + + + +document.write(batman.getInfo() + batman.makeSound() + "
"); +document.write(superman.getInfo() + superman.makeSound() + "
"); +document.write(superman === batman + "
"); +document.write(superman.sound === batman.sound); From b75b05b17ab610d94d57a24ce705b1c7c5d296e2 Mon Sep 17 00:00:00 2001 From: clenclen Date: Thu, 18 Feb 2016 17:15:11 -0500 Subject: [PATCH 2/3] cup added cup method to see functions of prototype are shared across instances --- js/super_hero.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/js/super_hero.js b/js/super_hero.js index a6f8817..750f09d 100644 --- a/js/super_hero.js +++ b/js/super_hero.js @@ -7,6 +7,11 @@ var SuperHero = function(name, power) { } } +SuperHero.prototype.cup = function() { + return this.name + ' ' + this.power; +} + + SuperHero.prototype.sound = "Bang"; SuperHero.prototype.makeSound = function(){ return " and says " + this.sound; @@ -21,5 +26,10 @@ var superman = new SuperHero("Superman", "super strength") document.write(batman.getInfo() + batman.makeSound() + "
"); document.write(superman.getInfo() + superman.makeSound() + "
"); -document.write(superman === batman + "
"); -document.write(superman.sound === batman.sound); + +document.write(superman.cup() + "
"); +document.write((superman.cup === batman.cup) + "
"); + +document.write((superman.name === batman.name) + "
"); + + From 069e9066ddda669ced467330b8d88a507cceb36a Mon Sep 17 00:00:00 2001 From: clenclen Date: Thu, 25 Feb 2016 16:47:30 -0500 Subject: [PATCH 3/3] superhuman add class and two subclasses --- js/super_hero.js | 58 +++++++++++++++++++++++++++++++++--------------- js/tests.js | 6 +++++ 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/js/super_hero.js b/js/super_hero.js index 750f09d..d7ff0dc 100644 --- a/js/super_hero.js +++ b/js/super_hero.js @@ -1,35 +1,57 @@ - -var SuperHero = function(name, power) { +var SuperHuman = function(name){ this.name = name; - this.power = power; - this.getInfo = function(){ - return this.name + " has " + this.power; - } + this.health = 100; } -SuperHero.prototype.cup = function() { - return this.name + ' ' + this.power; +SuperHuman.prototype.doublepunch = function(opp1, opp2) { + this.health -= 3 + opp1.health -= 10; + opp2.health -= 5; + document.write(this.name + " has superpunched both " + opp1.name + " and " + opp2.name + "
") } -SuperHero.prototype.sound = "Bang"; -SuperHero.prototype.makeSound = function(){ - return " and says " + this.sound; +SuperHuman.prototype.attack = function(opp) { + opp.health -= 10; + this.health -= 5; + document.write(this.name + " just hit " + opp.name + "
") + document.write(this.name + " used 5 energy and has " + this.health + " left." + "
") + document.write(opp.name + " loses 10 energy and has " + opp.health + " left." + "
" + "
") } +var SuperHero = function(name) { + SuperHuman.call(this) + this.type = "a hero" + this.name = name; +} -var batman = new SuperHero("Batman", "batarang"); -var superman = new SuperHero("Superman", "super strength") +SuperHero.prototype = Object.create(SuperHuman.prototype) +SuperHero.prototype.heal = function(){ + this.health += 8 + document.write(this.name + " has used healing power and now has " + this.health + " health
") +} +function SuperVillian(name) { + SuperHero.call(this) + this.type = "a villain" + this.name = name +} +SuperVillian.prototype = Object.create(SuperHuman.prototype) -document.write(batman.getInfo() + batman.makeSound() + "
"); -document.write(superman.getInfo() + superman.makeSound() + "
"); -document.write(superman.cup() + "
"); -document.write((superman.cup === batman.cup) + "
"); +var batman = new SuperHero("batman"); +var superman = new SuperHero("superman") +var joker = new SuperVillian("joker") -document.write((superman.name === batman.name) + "
"); +batman.attack(joker) +joker.attack(superman) +superman.heal() +joker.doublepunch(batman, superman) +document.write( "
" + "health results:" + "
") +document.write(batman.name + " is " + batman.type + " and has " + batman.health + " health left." + "
") +document.write(superman.name + " is " + superman.type + " and has " + superman.health + " health left." + "
") +document.write(joker.name + " is " + joker.type + " and has " + joker.health + " health left." + "
") diff --git a/js/tests.js b/js/tests.js index 445444e..2fb605f 100644 --- a/js/tests.js +++ b/js/tests.js @@ -2,4 +2,10 @@ QUnit.test("hello test", function(assert) { assert.strictEqual(1 + 1, 2, "One plus one is two"); }); + +QUnit.test("health matches", function(assert) { + assert.strictEqual(superman.health, superman.health , "health matches"); +}); + + // ADD TESTS HERE