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
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# dependencies
/node_modules

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

170 changes: 166 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/

function GameObject(attributes) {
this.createdAt = attributes.createdAt,
this.dimensions = attributes.dimensions
}

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


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

function CharacterStats (attributes) {
//Inherit GameObject attributes
GameObject.call(this, attributes)
this.healthPoints = attributes.healthPoints,
this.name = attributes.name
}

//Inherit GameObject prototypes
CharacterStats.prototype = Object.create(GameObject.prototype)

//Create a new prototype function
CharacterStats.prototype.takeDamage = function () {
return `${this.name} took damage.`
}


/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -39,9 +65,24 @@
* 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:
function Humanoid(attributes) {
CharacterStats.call(this, attributes)
this.team = attributes.team,
this.weapons = attributes.weapons,
this.language = attributes.language,
this.attackPoints = attributes.attackPoints,
this.specialAttack = attributes.specialAttack
}
//Inherit prototype functions from CharacterStats object
Humanoid.prototype = Object.create(CharacterStats.prototype)

/*
//Add prototype function
Humanoid.prototype.greet = function () {
return `${this.name} offers a greeting in ${this.language}.`
}


// Test your 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 +143,130 @@
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!

/*
=== Villain ===
Attributes:
* affliation: 'Villain

Prototype Functions:
* warCry()
* fireballThrow()
*/
function Villain (attributes) {
Humanoid.call(this, attributes)
this.affliation = 'Villain'
}

Villain.prototype = Object.create(Humanoid.prototype)
Villain.prototype.warCry = function () {
return `${this.affliation}s will conquer the world!`
}
//Add an attack method
Villain.prototype.fireballThrow = function (opponent) {
opponent.healthPoints = opponent.healthPoints - this.attackPoints
}

/*
=== Hero ===
Attributes:
* affliation: 'Hero'

Prototype Functions:
* battleCry()
* swordStrike()
*/
function Hero (attributes) {
Humanoid.call(this, attributes)
this.affliation = 'Hero'
}

Hero.prototype = Object.create(Humanoid.prototype)
Hero.prototype.battleCry = function () {
return `${this.affliation}es will save the world, YO JOE!!!`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't tell if this is an Easter Egg for me or a GI Joe reference! This is probably the best game I've yet seen for this project.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is a GI Joe, reference. I purposely mixed genres to see if anyone would notice. Haha!

}
//Add an attack method
Hero.prototype.swordStrike = function (opponent) {
opponent.healthPoints = opponent.healthPoints - this.attackPoints
}

const sorcerer = new Villain({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Raistlin',
team: 'Wizard of High Sorcery',
weapons: [
'Staff of Magius',
],
language: 'Common Tongue',
attackPoints: 2,
specialAttack: 'fireball spell'
});

const warrior = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Caramon',
team: 'Heroes of the Lance',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
attackPoints: 3,
specialAttack: 'sword strikes'
});

//Background story
function backStory (character) {
return `${character.name}, a ${character.affliation} and member of the \"${character.team}\"`
}
console.log(`We begin our journey with ${backStory(sorcerer)}; and with ${backStory(warrior)}.`)

//Battle Sequence
function combat (character, opponent) {
// Choose character attack
function attack () {
if (character.affliation === 'Villain') {
character.fireballThrow(opponent)
} else {
character.swordStrike(opponent)
}
}

// Execute attack
let motto = character.affliation === 'Villain' ? character.warCry() : character.battleCry()
attack()

// Show combat play by play and battle status
if (opponent.healthPoints <= 0) {
return `${character.name} shouts \"${motto}\" ${character.name} uses ${character.specialAttack} to defeat ${opponent.name}! ${opponent.destroy()}`
} else {
return `${character.name} shouts \"${motto}\" ${character.name} uses ${character.specialAttack} to attack ${opponent.name}. Leaving him with ${opponent.healthPoints} health points.`
}
}

console.log(combat(sorcerer, warrior))
console.log(combat(warrior, sorcerer))
console.log(combat(warrior, sorcerer))





58 changes: 50 additions & 8 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 (Default) Binding: this binds to the global object. It is the default setting.
* 2. Implicit Binding: occurs when dot notation is used to invoke a function.
Whatever is to the left of the dot becomes the context for ```this``` in the function.
* 3. Explicit Binding: occurs when .call(), .apply(), or .bind() are used on a function.
* 4. New Binding: occurs when building new objects or when explicitly binding an existing function to the object
*
* write out a code example of each explanation above
*/

// Principle 1

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

// Principle 2
function myWinBindFuncStrict () {
'use strict'
console.log(this)
}
myWinBindFuncStrict() //undefined

// Principle 2
// code example for Implicit Binding
let animal = {
species: 'dog',
name: 'Snoopy',
identify: function () {
console.log(`${this.name} is a ${this.species}`)
}
}
animal.identify()

// Principle 3

// code example for New Binding
// Use a 'constructor' which is a function that creates an object
function Person(name) {
this.name = name,
this.greeting = function () {
return `Hello, my name is ${this.name}`
}
}

let wilfred = new Person('Wilfred')
console.log(wilfred)
console.log(wilfred.greeting())

// Principle 4
// code example for Explicit Binding

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the best example of explicit binding I've seen. It's difficult for me to come up with a good reason to use it.

@wmemorgan wmemorgan Jan 24, 2019 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Joe!

To answer your comment:

It's difficult for me to come up with a good reason to use it.

This particular code allows you to combine an object and an array and dynamically iterate through the array (instead of listing item1, item2, item3, etc).

An example I could think of for using this code in real life for creating a mail merge of customer information into a marketing form letter. So if I take my code from Exercise bloominstituteoftechnology#4 and extend it for this example we've got something like this....

function Customer (name, package) {
  this.name = name,
  this.package = package
  this.greeting = function () {
    return `Hello ${this.name}`
  }
}
//Creating an object from the Customer constructor

const joe = new Customer('Joe', premium) //Premium Service Customer
//Create an array of a list of services
const premiumPackage = ['ESPN', 'Disney Channel', 'HBO', 'Cinemax', 'Showtime', 'Starz']

const wilfred = new Customer('Wilfred', basic) //Basic Service Customer
//Create an array of a list of services
const basicPackage = ['ESPN', 'Disney Channel']

// Automatically generate new customer welcome letter
function welcomeLetter (list) {
  return `${this.greeting()}, we see you've signed up for our ${this.package} channels: ${list.map((item, index) => index !== list.length-1 ?' '+ item: ' and ' + item)}.`
}
// Explicity bind the object and array to the introduce function using the .call() method
console.log(welcomeLetter.call(joe, premiumPackage))
console.log(welcomeLetter.call(wilfred, basicPackage))
`

Being able to do things like that will make you a hero with the marketing folks and management. Haha!

There's more you can do with this code but this shows the possible value of using explicit binding.

//Creating an object from the Person constructor in Principle #3
let george = new Person('George')
console.log(george)
console.log(george.greeting())

//Create an array for demonstration purposes
const hobbies = ['painting', 'scuba diving', 'golf', 'reading', 'cooking']

//Create a function that will use data from the newly created object and array
function introduce (list) {
return `${this.greeting()}, my hobbies are ${list.map((item, index) => index !== list.length-1 ?' '+ item: ' and ' + item)}.`
}
// Explicity bind the object and array to the introduce function using the .call() method
console.log(introduce.call(george, hobbies))

// code example for Explicit Binding