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
107 changes: 101 additions & 6 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,29 @@
* 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(attrs){
this.healthPoints = attrs.healthPoints;
GameObject.call(this, attrs);
}
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,16 +48,24 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attrs){
this.team = attrs.team;
this.weapons = attrs.weapons;
this.language = attrs.language;
CharacterStats.call(this, attrs);
}
Humanoid.prototype = Object.create(CharacterStats.prototype);

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.
*/

// 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 +126,80 @@
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 Villain(attrs){
Humanoid.call(this, attrs);
}
Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.Attack = function(object){
let damage = Math.floor((Math.random() * 5) + 1);
object.healthPoints = object.healthPoints - damage;
console.log(`%c ${badman.name} has dealt ${damage}`, `color: white; background: black;`);
}

function Hero(attrs){
Humanoid.call(this, attrs);
}
Hero.prototype = Object.create(Humanoid.prototype);

Hero.prototype.Attack = function(object){
let damage = Math.floor((Math.random() * 4) + 1);
object.healthPoints = object.healthPoints - damage;
console.log(`%c ${bro.name} has dealt ${damage}`, `color: white; background: blue;`);
}

const bro = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Zizane',
team: 'The Bois',
weapons: [
'Sword',
'Shield',
],
language: 'Bro Army',
});

const badman = new Villain({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 10,
name: 'Diavolo',
team: 'Mafia',
weapons: [
'King Crimson',
'Time Rift',
],
language: 'Common Tongue',
});

while(bro.healthPoints > 0 && badman.healthPoints > 0){
bro.Attack(badman);
if(badman.healthPoints <= 0)
break;
badman.Attack(bro);
}
console.log(`${bro.name} has ${bro.healthPoints} HP left`);
console.log(`${badman.name} has ${badman.healthPoints} HP left`);
if(bro.healthPoints > 0){
console.log(badman.destroy());
console.log(`${bro.name} has won`);
}
else {
console.log(bro.destroy());
console.log(`${badman.name} has won`);
}
26 changes: 19 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. window
* 2. object parent
* 3. function key definition
* 4. refence constructor
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

console.log(this);
//this will log the entire window object of the browser;
// Principle 2
const name = "bro";

const hero = {
name: "Nathan",
sayName : function(){console.log(this.name);}
//this this will look into the name of the object, so this would print Nathan not bro
}
// code example for Implicit Binding

// Principle 3

function ImAConstructor(attr){
this.name = attr.name;
this.child = arr.child; //this this tells the object that the name is a key value of the function.
}
// code example for New Binding

// Principle 4

function IAmAConstructorBeingPassed(attr){
ImAConstructor.call(this, attr); //this passes the function into the previous constructor
}
// code example for Explicit Binding