From 6bba634a9bc08bcd899fc2786cc5e8cf4ef38797 Mon Sep 17 00:00:00 2001 From: Joshua Humphrey Date: Thu, 12 Sep 2019 16:23:31 -0500 Subject: [PATCH] Completed both assignment sections --- assignments/prototypes.js | 118 +++++++++++++++++++++++++++++++++++++- assignments/this.js | 35 +++++++++-- 2 files changed, 144 insertions(+), 9 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..83ba7d9a8 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -15,6 +15,18 @@ * dimensions (These represent the character's size in the video game) * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(gameAttrs) { + this.createdAt = gameAttrs.createdAt; + this.name= gameAttrs.name; + this.dimensions= gameAttrs.dimensions; + + } +//Add destroy method to prototype. + GameObject.prototype.destroy = function() { + console.log(`${this.name} was removed from the game.`) +} +// console.log(GameObject.prototype); + /* === CharacterStats === @@ -22,6 +34,20 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats(stats) { + GameObject.call(this, stats); + this.healthPoints = stats.healthPoints; +} + +CharacterStats.prototype = Object.create(GameObject.prototype); +//Adds take damage method to prototype +CharacterStats.prototype.takeDamage = function () { + console.log(`${this.name} took damage!`) +} + + +// console.log(CharacterStats); +// console.log(CharacterStats.prototype); /* === Humanoid (Having an appearance or character resembling that of a human.) === @@ -32,6 +58,18 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ +function Humanoid(humanoidAttrs) { + CharacterStats.call(this, humanoidAttrs); + this.team = humanoidAttrs.team; + this.weapons = humanoidAttrs.weapons; + this.language = humanoidAttrs.language; + // console.log(this); +} +Humanoid.prototype = Object.create(CharacterStats.prototype); +// console.log(Humanoid.prototype); +Humanoid.prototype.greet = function(){ + console.log( `${this.name} offers a greeting in ${this.language}`); +} /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid @@ -41,7 +79,6 @@ // Test you work by un-commenting these 3 objects and the list of console logs below: -/* const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -102,9 +139,84 @@ 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 rollDice(amt,num,mod) { + let result =[]; + let total; + for(let i=0;i { + return accumulator + currentValue; + },0) + mod; + return total; + + + } + //This is a function I wrote a while back for a dice roller, it is designed for multiple dice. + + // function doDamage() { + // if(heroResult > bbegResult) { + // console.log('Hero does damage!') + // }else if(bbegResult > herResult) { + // console.log('BBEG does damage!') + // } + // } + + + let bbegResult = 0; + let heroResult = 0; + const bbeg = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 5 + }, + healthPoints: 20, + name: 'Craigward', + team: 'Evil Bois Inc.', + weapons: [ + 'Fireball', + 'Lightning Bolt' + ], + language: 'All' + }); + bbeg.roll = bbegResult = rollDice(1,20,0); + console.log(bbegResult); + + const hero = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 5 + }, + healthPoints: 20, + name: 'A. A. Ron', + team: 'Rent-a-Hero', + weapons: [ + 'Sick Insults', + 'These Hands' + ], + language: 'Common' + }); + hero.roll = heroResult = rollDice(1,20,0); + console.log(heroResult); + + + if(heroResult > bbegResult) { + console.log(bbeg.destroy()) + }else if(bbegResult > heroResult) { + console.log(hero.destroy()); + } + + + + + \ No newline at end of file diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..7ecc3202c 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,10 +1,10 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. Whenerver a function is contained in the global scope, the value of this inside the function will be the window object. +* 2. Whenever a function is called by a preceding dot, the object before the dot is this. +* 3. Whenever a constructor function i used, this refers to the specific instance of the object that is created and returned by the constructor function. +* 4. Whenever call or apply is used, this is explicitly defined. * * write out a code example of each explanation above */ @@ -12,15 +12,38 @@ // Principle 1 // code example for Window Binding - +function petDog(dog) { + console.log(this); + return dog; +} +petDog('Dot'); // Principle 2 // code example for Implicit Binding +const sayNameFunc = obj => { + obj.sayName = function() { + console.log(`Hello my name is ${this.name}`); + console.log(this); + } +}; + // Principle 3 // code example for New Binding +function Goblin(warrior) { + this.warcry = "WWAAHHHHHH!!!"; + this.warrior = warrior; + this.attack = function() { + console.log(`${this.warrior} shouts ${this.warcry}`); + } + +} +const gricc = new Goblin('Gricc'); +const torp = new Goblin('Torp'); // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +gricc.warrior.call(torp); +torp.warrior.call(gricc); \ No newline at end of file