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
141 changes: 128 additions & 13 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(go_arg) {
this.createdAt = go_arg.createdAt;
this.name = go_arg.name;
this.dimensions = go_arg.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(cs_arg) {
this.healthPoints = cs_arg.healthPoints;
GameObject.call(this,cs_arg);
}
//How do I check in console if inheritance is working correctly?
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,6 +52,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(hu_arg) {
this.team = hu_arg.team;
this.weapons = hu_arg.weapons;
this.language = hu_arg.language;
// Do I need both the lines below this, or just the second one?
GameObject.call(this,hu_arg);
CharacterStats.call(this,hu_arg);
}

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

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +75,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,19 +126,100 @@
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.
*/
// 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.
// * 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 Hero(he_arg) {
Humanoid.call(this,he_arg);
}
Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.attack = function(target) {
let random_num = Math.floor(Math.random() * 10) + 1;
let damage_done = target.healthPoints - random_num;
target.healthPoints = damage_done;
if (damage_done <= 0) {
return `${this.name} launches an attack on ${target.name} and does ${random_num} points of damage! ${target.name} was killed! ` + target.destroy();
} else {
return `${this.name} launches an attack on ${target.name} and does ${random_num} points of damage! ${target.name} now has ${damage_done} health points left.`;
}
}

function Villain(v_arg) {
Humanoid.call(this,v_arg);
}
Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.attack = function(target) {
let random_num = Math.floor(Math.random() * 10) + 1;
let damage_done = target.healthPoints - random_num;
target.healthPoints = damage_done;
if (damage_done <= 0) {
return `${this.name} launches an attack on ${target.name} and does ${random_num} points of damage! ${target.name} was killed! ` + target.destroy();
} else {
return `${this.name} launches an attack on ${target.name} and does ${random_num} points of damage! ${target.name} now has ${damage_done} health points left.`;
}
}

const evilGuy = new Villain ({
createdAt: new Date(),
dimensions: {
length: 5,
width: 3,
height: 9,
},
healthPoints: 18,
name: 'Evil Guy',
team: 'Doooooom',
weapons: [
'Axe',
'Mace',
],
language: 'Evil',

});

const heroGuy = new Hero ({
createdAt: new Date(),
dimensions: {
length: 2,
width: 8,
height: 5,
},
healthPoints: 13,
name: 'Hero Guy',
team: 'Good',
weapons: [
'Sword',
'Spear',
],
language: 'Normal',

});

console.log(swordsman.healthPoints);
console.log(evilGuy.attack(swordsman));
console.log(swordsman.healthPoints);
console.log(evilGuy.attack(swordsman));
console.log(evilGuy.attack(swordsman));

console.log(swordsman.healthPoints);
console.log(heroGuy.attack(archer));
console.log(swordsman.healthPoints);
console.log(heroGuy.attack(archer));
console.log(heroGuy.attack(archer));



51 changes: 46 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window Binding - The default context is the Window, so a basic function that is defined and called will be bound to the Window
* 2. Implicit Binding - With this type of binding we use dot notation to bind to the object that the function we're calling is stored in.
* 3. New Binding - When we use the new keyword we generate a new object based on the class constructor and that object is the binding for
this.
* 4. Explicit Binding - Here we use the functions call, apply and bind in order to force whatever function we're calling to use
another object of our choice for its binding.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +15,53 @@

// code example for Window Binding

function greetMe(name) {
console.log(`Hello ${name}`);
}

greetMe('Josh');

// Principle 2

// code example for Implicit Binding

const guy = {
name: "Jimothy",
height: 72,
announceHeight: function() {
return `${this.name} is ${this.height} inches tall.`;
}
}

console.log(guy.announceHeight());

// Principle 3

// code example for New Binding

function Person(name, height) {
this.name = name;
this.height = height;
this.announceHeight = function() {
return `${this.name} is ${this.height} inches tall.`;
}
}

const jimothy = new Person("Jimothy", 72);
console.log(jimothy.announceHeight());

// Principle 4

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

const person = {
name: "Jill"
}

const skills = ["HTML","CSS","JS"];

function introduce(skills1, skills2, skills3) {
return `Hello! my name is: ${this.name} and these are my skills: ${skills1}, ${skills2}, ${skills3}`;
}

console.log(introduce.call(person, skills));