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
188 changes: 176 additions & 12 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,33 @@
* 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
* 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);
CharacterStats.prototype.takeDamage = function() {
return `${this.name} took damage.`;
}

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,7 +52,114 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid (human) {
CharacterStats.call(this, human);
this.team = human.team;
this.weapons = human.weapons;
this.language = human.language;
}

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

// Hero

function Hero(heroAttrs) {
Humanoid.call(this, heroAttrs);
this.strength = function(){
return Math.floor(Math.random() * 10);
}
this.defense = function(){
return Math.floor(Math.random() * 10);
}
}

Hero.prototype = Object.create(Humanoid.prototype);

// Villian

function Villian(villainAttrs) {
Humanoid.call(this, villainAttrs);
this.power = function(){
return Math.floor(Math.random() * 10);
}
this.defense = function() {
return Math.floor(Math.random() * 10);
};
}

Villian.prototype = Object.create(Humanoid.prototype);

// Fight Function

function fight(hero, villian) {
let heroDefense = hero.defense;
let villianDefense = villian.defense;
let heroAttack = hero.strength;
let villainAttack = villian.power;
let heroHealth = hero.healthPoints;
let villianHealth = villian.healthPoints;
let villianName = villian.name;
let heroName = hero.name;

let first = Math.floor(Math.random() * 2);
let heroDefend = heroDefense();
let villianPower = villainAttack();
let heroPower = heroAttack();
let villianDefend = villianDefense();


// Fight Logic


while(heroHealth > 0 && villianHealth > 0) {
if(first == 0) {
if(villianPower > heroDefend) {
heroHealth -= villianPower;
console.log(`${villianName} did ${villianPower} damage to ${heroName}`);
if(heroHealth <= 0) {
return console.log(hero.destroy());
}else {
console.log(`${heroName} has ${heroHealth} health left`);
heroPower = heroAttack();
villianDefend = villianDefense();
first = 1;
}
}else if(heroDefend >= villianPower) {
console.log(`${villianName} tried to attack but ${heroName} blocked the move.`);
console.log(`${heroName} has ${heroHealth} health left`);
heroPower = heroAttack();
villianDefend = villianDefense();
first = 1;
}
}else {
if(heroPower > villianDefend) {
villianHealth -= heroPower;
console.log(`${heroName} did ${heroPower} damage to ${villianName}`);
if(villianHealth <= 0) {
return console.log(villian.destroy());
}else{
console.log(`${villianName} has ${villianHealth} health left`);
heroDefend = heroDefense();
villianPower = villainAttack();
first = 0;
}
}else if(villianDefend >= heroPower) {
console.log(`${heroName} tried to attack but ${villianName} blocked the move.`);
console.log(`${villianName} has ${villianHealth} health left`);
heroDefend = heroDefense();
villianPower = villainAttack();
first = 0;
}
}
}
}

// Hero Attack

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +168,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,17 +219,54 @@
language: 'Elvish',
});

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.
*/
const kobe = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 30,
name: 'Kobe Bryant',
team: 'LA Lakers',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

const lebron = new Villian({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 30,
name: 'Lebron James',
team: 'Cleveland Cavs',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

fight(kobe, lebron);


// 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.
Expand Down
36 changes: 31 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -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. Global Binding - this keyword refers to the global or window object
* 2. Implicit Binding - this keyword will refer to the object on the of dot notation
* 3. New Binding - this keyword refers to any new objects creted from a prototype
* 4. Explicit Binding - this keyword will refer to the object that is passed through apply(), bind() or call();
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,40 @@

// code example for Window Binding

console.log(this);

// Principle 2

// code example for Implicit Binding

const testObj = {
name: "Object",
print: function() {
return `I am a ${this.name}`;
}
}

testObj.print();

// Principle 3

// code example for New Binding

function TestObjects(name) {
this.name = name,
this.display = "Computer",
this.show = function() {
return `This is ${this.name} printing on your ${this.display}`
}
}

const buddy = new TestObjects("Buddy");
const alien = new TestObjects("Alien");
buddy.show();
alien.show();
// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

buddy.show.call(alien);
alien.show.apply(buddy);