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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ This challenge focuses on using the `this` keyword as well as getting comfortabl

**Follow these steps to set up and work on your project:**

* [ ] Create a forked copy of this project.
* [X] Create a forked copy of this project.
* [ ] Add your project manager as collaborator on Github.
* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [ ] Create a new branch: git checkout -b `<firstName-lastName>`.
* [ ] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [ ] Push commits: git push origin `<firstName-lastName>`.
* [X] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [X] Create a new branch: git checkout -b `<firstName-lastName>`.
* [X] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [X] Push commits: git push origin `<firstName-lastName>`.

**Follow these steps for completing your project.**

* [ ] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
* [X] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
* [ ] Add your project manager as a reviewer on the pull-request
* [ ] Your project manager will count the project as complete by merging the branch back into master.
* [X] Your project manager will count the project as complete by merging the branch back into master.

## Assignment Set Up

Expand Down
249 changes: 178 additions & 71 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Each constructor function has unique properties and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
Expand All @@ -32,79 +32,186 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

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

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`;
};

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

CharacterStats.prototype = Object.create(GameObject.prototype);

/* updated for stretch */
CharacterStats.prototype.takeDamage = function(damage) {

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.

great job with the damage logic.

this.healthPoints -= damage;
if(this.healthPoints <= 0) {
return `Critical damage! ` + this.destroy();
} else
return `${this.name} took damage. Health remaining: ${this.healthPoints}`;
};

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}`;
};

// Test you work by un-commenting these 3 objects and the list of console logs below:

/*
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
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.
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1
},
healthPoints: 5,
name: "Bruce",
team: "Mage Guild",
weapons: ["Staff of Shamalama"],
language: "Common Tongue"
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2
},
healthPoints: 15,
name: "Sir Mustachio",
team: "The Round Table",
weapons: ["Giant Sword", "Shield"],
language: "Common Tongue"
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4
},
healthPoints: 10,
name: "Lilith",
team: "Forest Kingdom",
weapons: ["Bow", "Dagger"],
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(5)); // 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!

/****************
* The takeDamage() method has been updated above for optimal implementation of this stretch
*
*/

// 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!
function Villain(attr) {
Humanoid.call(this, attr);
//this.super();

this.spitPotionAt = function (target){
console.log(`${this.name} spit potion at ${target.name}!`);
return target.takeDamage(5);
}
this.castSpellOn = function(target){
console.log(`${this.name} cast a spell on ${target.name}!`);
//regenerates health when this attack is used
this.healthPoints += 1;
return target.takeDamage(3);
}
}

Villain.prototype = Object.create(Humanoid.prototype);

function Hero(attr) {
Humanoid.call(this, attr);
//this.super();

this.swordStab = function (target){
console.log(`${this.name} stabbed ${target.name} with a sword!`);
return target.takeDamage(6);
}
this.airstrike = function (target) {
console.log(`${this.name} launched an airstrike on ${target.name}!`);
this.healthPoints += 2;
return target.takeDamage(2);

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.

only 2 damage for an airstrike?

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.

the AI that fires the missiles is inaccurate 🤷‍♂

}
}

Hero.prototype = Object.create(Humanoid.prototype);

const houdini = new Villain({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1
},
healthPoints: 15,
name: "Houdini",
team: "Leeches",
weapons: ["Annoying words"],
language: "Basic Tongue"
});

const momoney = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 2
},
healthPoints: 18,
name: "Mo Money",
team: "Seeds",
weapons: ["JavaScript code"],
language: "Full-Stack Tongue"
});

console.log(houdini.spitPotionAt(momoney));
console.log(momoney.airstrike(houdini));
console.log(houdini.castSpellOn(momoney));
console.log(momoney.swordStab(houdini));
console.log(houdini.castSpellOn(momoney));
console.log(momoney.swordStab(houdini));
console.log(houdini.spitPotionAt(momoney));
console.log(momoney.airstrike(houdini));
console.log(houdini.castSpellOn(momoney));
console.log(momoney.airstrike(houdini));
45 changes: 40 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
/* 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 means that the 'this' keyword binds to the window object (also
known as default binding)
* 2. Implicit binding means that the 'this' keyword binds implicitly (automatically)

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.

Implicit binding refers to the dot notaton. the object on the left of the dot is implicated as THIS.

obj.speak() obj is THIS

const obj = {
  name: 'steve',
  speak() {
    return `hello my name is ${this.name}`;
  }
}
obj.speak();

to its variable object.
* 3. Explicit binding is when the 'this' keyword is explicitly assigned to an object
of the programmer's choice
* 4. New binding happens when a new object is created, and the 'this' keyword gets
assigned to that empty object before it is initialized.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
console.log(this);

// Principle 2

// code example for Implicit Binding
function test (){
console.log(this.name);
}

const objTest = {
name: "Mo Money",
test: test
};

objTest.test();
// Principle 3

// code example for New Binding
function testFunc (){

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.

by convention, Constructor functions are named with CapitalCase. this does not affect the way the code functions, but it provides a helpful visual cue to keep your code standardized and thereby easier for you to read.

function TestFunc() {

this.name = "Mo";
this.speak = function (){
return "My name is " + this.name;
};
}

console.log(testFunc.name);
var testFuncTest = new testFunc ();

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.

you should only be using CONST and LET to declare variables. VAR is a relic and does not have the same helpful scoping rules that CONST and LET provide.
const testFuncTest = new TestFunc();

console.log(testFuncTest.name);

testFunc();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
function hello (){
console.log(this.text);
}

let explicitExample = {
text: "Test Text"
}

hello.call(explicitExample);