From 103e764ccfe3868b7dcdec3498833c6e8679e734 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 10 Oct 2019 20:32:44 -0400 Subject: [PATCH 1/2] first commit js error will fix later --- assignments/prototypes.js | 71 +++++++++++++++++++++++++++++++++++++-- assignments/this.js | 37 +++++++++++++++----- 2 files changed, 97 insertions(+), 11 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..0bbcd3830 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -16,6 +16,16 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject ( attrs) { + this.createdAt = attrs.createdAt; + this.name = attrs.name; + this.dimensions = attrs.dimensions ; +} + +GameObject.prototype.destroy = function ( ){ + return `${this.name} was removed from the game.`; +} + /* === CharacterStats === * healthPoints @@ -23,6 +33,17 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats ( attrs) { + GameObject.call(this,attrs) + this.healthPoints = attrs.healthPoints; +} + +CharacterStats.prototype = Object.create(GameObject.prototype); + +CharacterStats.prototype.takeDamage = function () { + return `${this.name} took damage`; +} + /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -32,6 +53,19 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ + +function Humanoid (attrs) { + CharacterStats.call(this,attrs); + this.team = attrs.team; + this.weapons = attrs.weapons; + this.language = attrs.language; +} + +Humanoid.prototype = Object.create(CharacterStats.prototype); + +Humanoid.prototype.greet = function () { + return `${this.name} offers a greeting in ${this.language}`; +} /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid @@ -41,7 +75,7 @@ // Test you work by un-commenting these 3 objects and the list of console logs below: -/* + const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -92,6 +126,7 @@ language: 'Elvish', }); + console.log(swordsman); console.log(mage.createdAt); // Today's date console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } console.log(swordsman.healthPoints); // 15 @@ -102,9 +137,39 @@ console.log(archer.greet()); // Lilith offers a greeting in Elvish. console.log(mage.takeDamage()); // Bruce took damage. console.log(swordsman.destroy()); // Sir Mustachio was removed from the game. -*/ + // Stretch task: // * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function. // * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0; - // * Create two new objects, one a villain and one a hero and fight it out with methods! \ No newline at end of file + // * Create two new objects, one a villain and one a hero and fight it out with methods! + + function Villain(attrs){ + Humanoid.call(this,attrs); + this.healthPoints = attrs.healthPoints; + } + + Villain.prototype = Object.create(Humanoid.prototype); + Villain.prototype.kryptonite = function() { + + for ( i = this.healthPoints; i > 0; i--){ + console.log(i); + if (i < 2) { + return this.destroy(); + } + } + } + + const vill = new Villain({healthPoints:10}); + console.log(vill); + console.log(vill.kryptonite()); + + + Villain.prototype = Object.create(GameObject.prototype); + + function Hero (attrs) { + Humanoid.call(this,attrs); + superPower = function () { + + } + } \ No newline at end of file diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..73ee0e8e3 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,47 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. Window/Global Object Binding +* 2. Implicit Binding +* 3. New Binding +* 4. Explicit Binding * * write out a code example of each explanation above */ // Principle 1 -// code example for Window Binding +var person = { + firstName: "John", + lastName : "Doe", + id : 5566, + fullName : function() { + return this.firstName + " " + this.lastName; + } + }; // Principle 2 -// code example for Implicit Binding +fullName : function() { + return this.firstName + " " + this.lastName; + } // Principle 3 -// code example for New Binding +function myFunction() { + return this; + } // Principle 4 -// code example for Explicit Binding \ No newline at end of file +var person1 = { + fullName: function() { + return this.firstName + " " + this.lastName; + } + } + var person2 = { + firstName:"John", + lastName: "Doe", + } + + console.log(person1.fullName.call(person2)); \ No newline at end of file From f67fea4e2c634ef458ccfc15d52e5f8f6bd7e6e2 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 11 Oct 2019 21:04:16 -0400 Subject: [PATCH 2/2] finish stretch goals --- assignments/prototypes.js | 240 ++++++++++++++++++++------------------ 1 file changed, 126 insertions(+), 114 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 0bbcd3830..507253f5d 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -7,7 +7,7 @@ Each constructor function has unique properties and methods that are defined in their block comments below: */ - + /* === GameObject === * createdAt @@ -16,15 +16,15 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ -function GameObject ( attrs) { +function GameObject(attrs) { this.createdAt = attrs.createdAt; this.name = attrs.name; - this.dimensions = attrs.dimensions ; + this.dimensions = attrs.dimensions; } -GameObject.prototype.destroy = function ( ){ +GameObject.prototype.destroy = function() { return `${this.name} was removed from the game.`; -} +}; /* === CharacterStats === @@ -33,16 +33,16 @@ GameObject.prototype.destroy = function ( ){ * should inherit destroy() from GameObject's prototype */ -function CharacterStats ( attrs) { - GameObject.call(this,attrs) - this.healthPoints = attrs.healthPoints; +function CharacterStats(attrs) { + GameObject.call(this, attrs); + this.healthPoints = attrs.healthPoints; } CharacterStats.prototype = Object.create(GameObject.prototype); -CharacterStats.prototype.takeDamage = function () { +CharacterStats.prototype.takeDamage = function() { return `${this.name} took damage`; -} +}; /* === Humanoid (Having an appearance or character resembling that of a human.) === @@ -54,8 +54,8 @@ CharacterStats.prototype.takeDamage = function () { * should inherit takeDamage() from CharacterStats */ -function Humanoid (attrs) { - CharacterStats.call(this,attrs); +function Humanoid(attrs) { + CharacterStats.call(this, attrs); this.team = attrs.team; this.weapons = attrs.weapons; this.language = attrs.language; @@ -63,113 +63,125 @@ function Humanoid (attrs) { Humanoid.prototype = Object.create(CharacterStats.prototype); -Humanoid.prototype.greet = function () { +Humanoid.prototype.greet = function() { return `${this.name} offers a greeting in ${this.language}`; -} - +}; + /* - * Inheritance chain: GameObject -> CharacterStats -> Humanoid - * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. - * Instances of CharacterStats should have all of the same properties as GameObject. -*/ + * Inheritance chain: GameObject -> CharacterStats -> Humanoid + * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. + * Instances of CharacterStats should have all of the same properties as GameObject. + */ // Test you work by un-commenting these 3 objects and the list of console logs below: +const mage = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + healthPoints: 5, + name: "Bruce", + team: "Mage Guild", + weapons: ["Staff of Shamalama"], + language: "Common Tongue", +}); + +const swordsman = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 2, + height: 2, + }, + healthPoints: 15, + name: "Sir Mustachio", + team: "The Round Table", + weapons: ["Giant Sword", "Shield"], + language: "Common Tongue", +}); + +const archer = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4, + }, + healthPoints: 10, + name: "Lilith", + team: "Forest Kingdom", + weapons: ["Bow", "Dagger"], + language: "Elvish", +}); + +// console.log(swordsman); +// console.log(mage.createdAt); // Today's date +// console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } +// console.log(swordsman.healthPoints); // 15 +// console.log(mage.name); // Bruce +// console.log(swordsman.team); // The Round Table +// console.log(mage.weapons); // Staff of Shamalama +// console.log(archer.language); // Elvish +// console.log(archer.greet()); // Lilith offers a greeting in Elvish. +// console.log(mage.takeDamage()); // Bruce took damage. +// console.log(swordsman.destroy()); // Sir Mustachio was removed from the game. + +// Stretch task: +// * Create Villain and hero constructor functions that inherit from the Humanoid constructor function. +// * Give the hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0; +// * Create two new objects, one a villain and one a hero and fight it out with methods! + +function Villain(attrs) { + Humanoid.call(this, attrs); + this.speciaWeapon = attrs.speciaWeapon; +} - const mage = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 1, - height: 1, - }, - healthPoints: 5, - name: 'Bruce', - team: 'Mage Guild', - weapons: [ - 'Staff of Shamalama', - ], - language: 'Common Tongue', - }); - - const swordsman = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 2, - height: 2, - }, - healthPoints: 15, - name: 'Sir Mustachio', - team: 'The Round Table', - weapons: [ - 'Giant Sword', - 'Shield', - ], - language: 'Common Tongue', - }); - - const archer = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 1, - width: 2, - height: 4, - }, - healthPoints: 10, - name: 'Lilith', - team: 'Forest Kingdom', - weapons: [ - 'Bow', - 'Dagger', - ], - language: 'Elvish', - }); - - console.log(swordsman); - console.log(mage.createdAt); // Today's date - console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } - console.log(swordsman.healthPoints); // 15 - console.log(mage.name); // Bruce - console.log(swordsman.team); // The Round Table - console.log(mage.weapons); // Staff of Shamalama - console.log(archer.language); // Elvish - console.log(archer.greet()); // Lilith offers a greeting in Elvish. - console.log(mage.takeDamage()); // Bruce took damage. - console.log(swordsman.destroy()); // Sir Mustachio was removed from the game. - - - // Stretch task: - // * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function. - // * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0; - // * Create two new objects, one a villain and one a hero and fight it out with methods! - - function Villain(attrs){ - Humanoid.call(this,attrs); - this.healthPoints = attrs.healthPoints; +Villain.prototype = Object.create(Humanoid.prototype); +Villain.prototype.kryptonite = function(hero) { + hero.healthPoints = hero.healthPoints - 2; + if (hero.healthPoints <= 0) { + return `${this.name} uses ${this.speciaWeapon} and ${hero.destroy()}`; + } else { + return `${this.name} cast kryptonite ${hero.healthPoints} hp left`; } +}; + +const vill = new Villain({ + healthPoints: 12, + name: "xor", + speciaWeapon: "fire", +}); + +console.log(vill); - Villain.prototype = Object.create(Humanoid.prototype); - Villain.prototype.kryptonite = function() { - - for ( i = this.healthPoints; i > 0; i--){ - console.log(i); - if (i < 2) { - return this.destroy(); - } - } - } - - const vill = new Villain({healthPoints:10}); - console.log(vill); - console.log(vill.kryptonite()); - - - Villain.prototype = Object.create(GameObject.prototype); - - function Hero (attrs) { - Humanoid.call(this,attrs); - superPower = function () { - - } - } \ No newline at end of file +function Hero(attrs) { + Humanoid.call(this, attrs); + this.speciaWeapon = attrs.speciaWeapon; +} + +Hero.prototype = Object.create(Humanoid.prototype); +Hero.prototype.SuperPower = function(villain) { + villain.healthPoints = villain.healthPoints - 4; + if (villain.healthPoints <= 0) { + return `${this.name} uses ${this.speciaWeapon} and ${villain.destroy()}`; + } else { + return `${this.name} cast kryptonite ${villain.healthPoints} hp left`; + } +}; + +const superHero = new Hero({ + healthPoints: 12, + name: "Zabb", + speciaWeapon: "ice", +}); + +console.log(superHero); +console.log(vill.kryptonite(superHero)); +console.log(superHero.SuperPower(vill)); +console.log(vill.kryptonite(superHero)); +console.log(superHero.SuperPower(vill)); +console.log(vill.kryptonite(superHero)); +console.log(superHero.SuperPower(vill)); \ No newline at end of file