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
6 changes: 6 additions & 0 deletions assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@

<body>
<h1>JS III - Check your work in the console!</h1>
<button onClick="window.location.reload();">Restart Battle</button>
<p id="villainText">Villain Output</p>
<button onclick="archerVillain.attackVillain(archerHero)">Villain Attack</button>
<hr>
<p id="heroText">Hero Output</p>
<button onclick="archerHero.attackHero(archerVillain)">Hero Attack</button>
</body>
</html>
165 changes: 161 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/
console.log(``)

function GameObject(attr) {
this.createdAt = attr.createdAt;
this.name = attr.name;
this.dimensions = attr.dimensions;
}

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

/*
=== CharacterStats ===
Expand All @@ -23,6 +34,16 @@
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats(attr) {
GameObject.call(this, attr)
this.healthPoints = attr.healthPoints;
}

CharacterStats.prototype = Object.create(GameObject.prototype);
CharacterStats.prototype.takeDamage = function () {
return `${this.name} took damage, their health is now ${this.healthPoints - 1}`
}

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


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

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.
Expand All @@ -41,7 +74,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 @@ -102,9 +135,133 @@
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!

const rng = function () {
const number = Math.floor(Math.random() * 11) + 1;
if(number <= 5) {
return true;
}
else {
return false;
}
}

function Villain(attr) {
Humanoid.call(this, attr)
}

Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.attackVillain = function (obj) {
let healthVillain = obj.healthPoints;
if(healthVillain < 0) {
return ``
}
if(this.healthPoints <= 0) {
return document.getElementById("villainText").innerHTML = `You can't attack, you have died!`
}
if(rng() == true) {
obj.healthPoints = healthVillain - 2;
}
else {
obj.healthPoints = healthVillain - 1;
}

if(obj.healthPoints < 0) {
return document.getElementById("villainText").innerHTML = `You have destroyed ${obj.name}.`
}
else {
return document.getElementById("villainText").innerHTML = `${obj.name} has taken damage from ${this.name}, they now have ${healthVillain} health.`
}
}

function Hero(attr) {
Humanoid.call(this, attr)
}

Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.attackHero = function (obj) {
let healthHero = obj.healthPoints;
if(healthHero < 0) {
return ``
}
if(this.healthPoints <= 0) {
return document.getElementById("heroText").innerHTML = `You can't attack, you have died!`
}
if(rng() == true) {
obj.healthPoints = healthHero - 2;
}
else {
obj.healthPoints = healthHero - 1;
}

if(obj.healthPoints <= 0) {
return document.getElementById("heroText").innerHTML = `You have destroyed ${obj.name}.`
}
else {
return document.getElementById("heroText").innerHTML = `${obj.name} has taken damage from ${this.name}, they now have ${healthHero} health.`
}

}

const archerVillain = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Villain',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

const archerHero = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Hero',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

document.getElementById("demo").innerHTML = archerHero.attackHero(archerVillain);
// const fightLoop = function (hero,villain) {

// }


// console.log(archerVillain.attackVillain(archerHero));
// console.log(archerHero.attackHero(archerVillain));
// console.log(archerVillain.attackVillain(archerHero));
// console.log(archerHero.attackHero(archerVillain));
// console.log(archerVillain.attackVillain(archerHero));
// console.log(archerHero.attackHero(archerVillain));
// console.log(archerVillain.attackVillain(archerHero));
// console.log(archerHero.attackHero(archerVillain));
// console.log(archerVillain.attackVillain(archerHero));
// console.log(archerHero.attackHero(archerVillain));
// console.log(archerVillain.attackVillain(archerHero));
// console.log(archerHero.attackHero(archerVillain));
// console.log(archerVillain.attackVillain(archerHero));
// console.log(archerHero.attackHero(archerVillain));
// console.log(archerVillain.attackVillain(archerHero));
// console.log(archerHero.attackHero(archerVillain));
52 changes: 47 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,68 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global Object Binding, 'this' is in the global scope
* 2. When 'this' has a dot after it with some type of variable after that dot
* 3. Using a constructor function, it has a function with a capital letter at the start, and a 'new' statement when using that function
* 4. Using .call or .apply
*
* write out a code example of each explanation above
*/

// Principle 1

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

sayName('Andrew');

// Principle 2

// code example for Implicit Binding
const myObjOne = {
greeting: 'Hello',
sayHello: function(name) {
console.log(`${this.greeting} my name is ${name}`);
console.log(this);
}
};
myObjOne.sayHello('Andrew');

// Principle 3

// code example for New Binding
function Person(attr) {
this.greeting = attr.greeting;
this.name = attr.name;
this.speak = function() {
console.log(`${this.greeting} ${this.name}!`);
console.log(this);
}
}

const myObjTwo = {
greeting: 'Hello',
name: 'Andrew'
}

const andrew = new Person(myObjTwo);
andrew.speak();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
const yourObject = {
name: 'Andrew',
city: 'Houston',
favoriteFood: '🍕'
}
const thingsYouEnjoy = ['Nature', 'Coding', 'Games']

function tellUsAboutYourself(thing1, thing2, thing3){
return `Hi! My name is ${this.name}, I live in ${this.city}, and I enjoy ${thing1}, ${thing2}, and ${thing3}. I love to eat ${this.favoriteFood}.`
}

console.log(tellUsAboutYourself.apply(yourObject, thingsYouEnjoy))