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
130 changes: 127 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(attributes) {
this.createdAt = attributes.createdAt;
this.name = attributes.name;
this.dimensions = attributes.dimensions;
}
GameObject.prototype.destroy = function() {
return `${this.name} was removed from the game.`
}

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(attributes) {
GameObject.call(this, attributes);
this.healthPoints = attributes.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.) ===
Expand All @@ -32,7 +49,64 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attributes) {
CharacterStats.call(this, attributes);
this.team = attributes.team;
this.weapons = attributes.weapons;
this.language = attributes.language;
}
Humanoid.prototype = Object.create(CharacterStats.prototype);
Humanoid.prototype.greet = function() {
return `${this.name} offers a greeting in ${this.language}`;
}

function Hero(attributes) {
Humanoid.call(this, attributes);
}
// ==== NEW PROTOS ====
Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.attack = function(enemy) {
if (enemy.healthPoints > 0) {
if (this.healthPoints > 0) {
enemy.healthPoints -= (Math.floor(Math.random() * 26) + 1);
if (enemy.healthPoints > 0) {
weaponOfChoice = Math.floor(Math.random() * this.weapons.length);
return `${this.name} swings ${this.weapons[weaponOfChoice]} at ${enemy.name}. ${enemy.name} is down to ${enemy.healthPoints} hp!`;
} else {
return `${enemy.name} has perished!`;
}
} else {
return `${this.name} is dead.`;
}
} else {
return `${this.name} was victorious!`;
}
}

function Villain(attributes) {
Humanoid.call(this, attributes);

}

Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.attack = function(enemy) {
if (enemy.healthPoints > 0) {
if (this.healthPoints > 0) {
enemy.healthPoints -= (Math.floor(Math.random() * 26) + 1);
if (enemy.healthPoints > 0) {
weaponOfChoice = Math.floor(Math.random() * this.weapons.length);
return `${this.name} casts ${this.weapons[weaponOfChoice]} at ${enemy.name}. ${enemy.name} is down to ${enemy.healthPoints} hp!`;
} else {
return `${enemy.name} has perished!`;
}
} else {
return `${this.name} is dead.`;
}
} else {
return `${this.name} was victorious!`;
}
}

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +115,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: {
Expand Down Expand Up @@ -92,6 +166,41 @@
language: 'Elvish',
});

// ==== NEW INSTANCES ====
const necromancer = new Villain({
createdAt: new Date(),
dimensions: {
length: 3,
width: 2,
height: 5,
},
healthPoints: 50,
name: 'Morag',
team: 'Mines',
weapons: [
'poison cloud',
'fireball',
],
language: 'Orcish',
});

const paladin = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 5,
height: 1,
},
healthPoints: 50,
name: 'Thorin',
team: 'Mountain',
weapons: [
'Hammer',
'Axe',
],
language: 'Dwarvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
Expand All @@ -102,7 +211,22 @@
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.
*/

console.log(necromancer.name);
console.log(necromancer.healthPoints);
console.log(paladin.attack(necromancer));
console.log(necromancer.attack(paladin));
console.log(paladin.attack(necromancer));
console.log(necromancer.attack(paladin));
console.log(paladin.attack(necromancer));
console.log(necromancer.attack(paladin));
console.log(paladin.attack(necromancer));
console.log(necromancer.attack(paladin));
console.log(paladin.attack(necromancer));
console.log(necromancer.attack(paladin));
console.log(paladin.attack(necromancer));
console.log(necromancer.attack(paladin));


// Stretch task:
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
Expand Down
30 changes: 25 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. "this" refers to it's parent object so when it's not bound to something else it defaults to reference the Window
* 2. Implicit binding refers to dot notation being use to invoke a function. So it implies that the item bound to the right of of the dot inherits context from what's to the left
* 3. New lets you create a new instance of an object that allows for variation without altering the original
* 4. explicit binding is adding "this" through methods like .call() or .apply()
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function blah() {
console.log(this);
}
blah();


// Principle 2

// code example for Implicit Binding
function parent(name) {
this.name = name;
}

// Principle 3

// code example for New Binding
let mary = new parent("Mary");
console.log(mary.name);

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
let randomObj = {
thingOne: "thing 1",
thingTwo: "thing 2",
thingThree: "thing 3"
};
let otherArr = ['index1', 'index2', 'index3']
function otherThings(param1, param2, param3) {
return `${this.thingOne} ${param1} ${param2} ${this.thingTwo} ${param3} ${this.thingThree}`
}
console.log(otherThings.apply(randomObj, otherArr));