Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 115 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,39 @@
* 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 ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> 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.) ===
Expand All @@ -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
Expand All @@ -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: {
Expand Down Expand Up @@ -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!
// * 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<amt;i++){
result.push(Math.floor(Math.random() * num + 1));
}
total = result.reduce((accumulator, currentValue)=> {
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());
}





35 changes: 29 additions & 6 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
/* 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
*/

// 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
// code example for Explicit Binding
gricc.warrior.call(torp);
torp.warrior.call(gricc);