Repository navigation
branched and answered a couple questions #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,22 @@ | |
| * destroy() // prototype method -> returns the string: 'Object was removed from the game.' | ||
| */ | ||
|
|
||
| // function Parent(attributes) { | ||
| // this.age = attributes.age; | ||
| // this.location = attributes.location; | ||
| // this.name = attributes.name; | ||
| // this.phrase = attributes.phrase; | ||
| // } | ||
|
|
||
| const GameObject = function(attributes) { | ||
| this.createdAt = attributes.createdAt; | ||
| this.dimensions = attributes.dimensions; | ||
| } | ||
|
|
||
| GameObject.prototype.destroy = function() { | ||
| return "Object was removed from the game."; | ||
| } | ||
|
|
||
| /* | ||
| === CharacterStats === | ||
| * healthPoints | ||
|
|
@@ -23,6 +39,18 @@ | |
| * should inherit destroy() from GameObject's prototype | ||
| */ | ||
|
|
||
| const CharacterStats = function(attributes) { | ||
| GameObject.call(this, attributes); | ||
| this.healthPoints = attributes.healthPoints; | ||
| this.name = attributes.name; | ||
| } | ||
|
|
||
| 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 | ||
|
|
@@ -32,6 +60,20 @@ | |
| * should inherit destroy() from GameObject through CharacterStats | ||
| * should inherit takeDamage() from CharacterStats | ||
| */ | ||
|
|
||
| const Humanoid = function(attributes) { | ||
| CharacterStats.call(this, attributes); | ||
| this.team = attributes.team; | ||
| this.weapons = attributes.weapons; | ||
| this.language = attributes.language; | ||
| }; | ||
|
|
||
| Humanoid.prototype = Object.create(CharacterStats.prototype); | ||
| Humanoid.prototype.greet = function() { | ||
| return `${this.name} offers a greeting in ${this.language}` | ||
| }; | ||
|
|
||
| console.log(Humanoid); | ||
|
|
||
| /* | ||
| * Inheritance chain: GameObject -> CharacterStats -> Humanoid | ||
|
|
@@ -41,7 +83,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: { | ||
|
|
@@ -102,9 +144,109 @@ | |
| 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! | ||
|
|
||
|
|
||
| //Hero boi | ||
| const Hero = function(attributes) { | ||
| Humanoid.call(this, attributes); | ||
| } | ||
|
|
||
| Hero.prototype = Object.create(Humanoid.prototype); | ||
| Hero.prototype.attack = function(target) { | ||
| if(this.healthPoints > 0) { | ||
| if(target.healthPoints > 0) { | ||
| let damage = (Math.round(Math.random() * 10)); | ||
| target.healthPoints = target.healthPoints - damage; | ||
| if (target.healthPoints > 0) { | ||
| console.log(`${this.name} attacked ${target.name} for ${damage} points! ${target.name} now has ${target.healthPoints} health`) | ||
| } else { | ||
| console.log(`${this.name} attacked ${target.name} for ${damage} points and destroyed ${target.name}!`) | ||
| } | ||
| } else { | ||
| console.log(`${this.name} kicked ${target.name}'s dead body`) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol |
||
| } | ||
| } else { | ||
| console.log(`${this.name}'s body twitched`) | ||
| } | ||
| } | ||
|
|
||
| //Villain boi | ||
| const Villain = function(attributes) { | ||
| Humanoid.call(this, attributes); | ||
| } | ||
|
|
||
| Villain.prototype = Object.create(Humanoid.prototype); | ||
| Villain.prototype.attack = function(target) { | ||
| if(this.healthPoints > 0) { | ||
| if(target.healthPoints > 0) { | ||
| let damage = (Math.round(Math.random() * 10)); | ||
| target.healthPoints = target.healthPoints - damage; | ||
| if (target.healthPoints > 0) { | ||
| console.log(`${this.name} attacked ${target.name} for ${damage} points! ${target.name} now has ${target.healthPoints} health`) | ||
| } else { | ||
| console.log(`${this.name} attacked ${target.name} for ${damage} points and destroyed ${target.name}!`) | ||
| } | ||
| } else { | ||
| console.log(`${this.name} kicked ${target.name}'s dead body`) | ||
| } | ||
| } else { | ||
| console.log(`${this.name}'s body twitched`) | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| const aragorn = new Hero({ | ||
| createdAt: new Date(), | ||
| dimensions: { | ||
| length: 1, | ||
| width: 1, | ||
| height: 2, | ||
| }, | ||
| healthPoints: 20, | ||
| name: 'Aragorn', | ||
| team: 'Ranger', | ||
| weapons: [ | ||
| 'Andúril', | ||
| ], | ||
| language: 'The laguage of man', | ||
| }); | ||
|
|
||
| const lurtz = new Villain({ | ||
| createdAt: new Date(), | ||
| dimensions: { | ||
| length: 1, | ||
| width: 1, | ||
| height: 2, | ||
| }, | ||
| healthPoints: 20, | ||
| name: 'Lurtz', | ||
| team: 'Uruk-hai', | ||
| weapons: [ | ||
| 'Bow', | ||
| ], | ||
| language: 'black speech', | ||
| }); | ||
|
|
||
| lurtz.attack(aragorn); | ||
| aragorn.attack(lurtz); | ||
| lurtz.attack(aragorn); | ||
| aragorn.attack(lurtz); | ||
| lurtz.attack(aragorn); | ||
| aragorn.attack(lurtz); | ||
| lurtz.attack(aragorn); | ||
| aragorn.attack(lurtz); | ||
| lurtz.attack(aragorn); | ||
| aragorn.attack(lurtz); | ||
| lurtz.attack(aragorn); | ||
| aragorn.attack(lurtz); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,20 @@ | ||
| /* 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 describes a trait of this where if you dont say where you want this to | ||
| go it will automtically revert to the window or the global scope depending on whether or | ||
| not it is pointing at anything | ||
|
|
||
| * 2. implicit binding deals with objects and allows this to reach into it's parent object to | ||
| find the vale of what it's referencing. | ||
|
|
||
| * 3. explicit binding is similar to implicit in that it is grabbing something within itself | ||
| except that it is grabbing paramaters passed into the function it exists in instead og keys in | ||
| its object | ||
|
|
||
| * 4. new binding refers to using the this keyword in object constructors and it's used to | ||
| create key value pairs within the function that can be assigned to new objects based on the | ||
| parameters passed into the function | ||
| * | ||
| * write out a code example of each explanation above | ||
| */ | ||
|
|
@@ -13,14 +23,60 @@ | |
|
|
||
| // code example for Window Binding | ||
|
|
||
| const name = { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Window Binding is just using |
||
| firstName: 'Jacob', | ||
| lastName: 'Angulo' | ||
| } | ||
|
|
||
| console.log(name.firstName + ' ' + name.lastName) | ||
|
|
||
| // Principle 2 | ||
|
|
||
| // code example for Implicit Binding | ||
|
|
||
| const me = { | ||
| firstName: 'Jacob', | ||
| lastName: 'Angulo', | ||
| speak: function() { | ||
| console.log(this.firstName + ' ' + this.lastName) | ||
| } | ||
| } | ||
|
|
||
| me.speak(); | ||
|
|
||
| // Principle 3 | ||
|
|
||
| // code example for New Binding | ||
|
|
||
| const NameConstuctor = function(param) { | ||
| this.firstName = param.firstName; | ||
| this.lastName = param.lastName | ||
| } | ||
|
|
||
| NameConstuctor.prototype.consoleFullName = function() { | ||
| console.log(`${this.firstName} ${this.lastName}`) | ||
| } | ||
|
|
||
| const miNombre = new NameConstuctor({ | ||
| firstName: 'Jacob', | ||
| lastName: 'Angulo' | ||
| }); | ||
|
|
||
| miNombre.consoleFullName(); | ||
|
|
||
| // Principle 4 | ||
|
|
||
| // code example for Explicit Binding | ||
| // code example for Explicit Binding | ||
|
|
||
| function sayName() { | ||
| console.log(this.firstName + ' ' + this.lastName) | ||
| } | ||
|
|
||
| const mePt2 = { | ||
| firstName: 'Jacob', | ||
| lastName: 'Angulo' | ||
| }; | ||
|
|
||
| const myNameAgain = sayName.bind(mePt2); | ||
|
|
||
| myNameAgain(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use the
destroyandtakeDamagefunctions you already have for these