Skip to content

Commit bb4d64d

Browse files
authored
Merge pull request #1 from jacobAngulo/jacob-angulo
branched and answered a couple questions
2 parents 1eb55ed + e496de0 commit bb4d64d

2 files changed

Lines changed: 206 additions & 8 deletions

File tree

‎assignments/prototypes.js‎

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
1616
*/
1717

18+
// function Parent(attributes) {
19+
// this.age = attributes.age;
20+
// this.location = attributes.location;
21+
// this.name = attributes.name;
22+
// this.phrase = attributes.phrase;
23+
// }
24+
25+
const GameObject = function(attributes) {
26+
this.createdAt = attributes.createdAt;
27+
this.dimensions = attributes.dimensions;
28+
}
29+
30+
GameObject.prototype.destroy = function() {
31+
return "Object was removed from the game.";
32+
}
33+
1834
/*
1935
=== CharacterStats ===
2036
* healthPoints
@@ -23,6 +39,18 @@
2339
* should inherit destroy() from GameObject's prototype
2440
*/
2541

42+
const CharacterStats = function(attributes) {
43+
GameObject.call(this, attributes);
44+
this.healthPoints = attributes.healthPoints;
45+
this.name = attributes.name;
46+
}
47+
48+
CharacterStats.prototype = Object.create(GameObject.prototype);
49+
50+
CharacterStats.prototype.takeDamage = function() {
51+
return `${this.name} took damage`
52+
}
53+
2654
/*
2755
=== Humanoid (Having an appearance or character resembling that of a human.) ===
2856
* team
@@ -32,6 +60,20 @@
3260
* should inherit destroy() from GameObject through CharacterStats
3361
* should inherit takeDamage() from CharacterStats
3462
*/
63+
64+
const Humanoid = function(attributes) {
65+
CharacterStats.call(this, attributes);
66+
this.team = attributes.team;
67+
this.weapons = attributes.weapons;
68+
this.language = attributes.language;
69+
};
70+
71+
Humanoid.prototype = Object.create(CharacterStats.prototype);
72+
Humanoid.prototype.greet = function() {
73+
return `${this.name} offers a greeting in ${this.language}`
74+
};
75+
76+
console.log(Humanoid);
3577

3678
/*
3779
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
@@ -41,7 +83,7 @@
4183

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

44-
/*
86+
4587
const mage = new Humanoid({
4688
createdAt: new Date(),
4789
dimensions: {
@@ -102,9 +144,109 @@
102144
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
103145
console.log(mage.takeDamage()); // Bruce took damage.
104146
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
105-
*/
147+
106148

107149
// Stretch task:
108150
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
109151
// * 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;
110-
// * Create two new objects, one a villain and one a hero and fight it out with methods!
152+
// * Create two new objects, one a villain and one a hero and fight it out with methods!
153+
154+
155+
//Hero boi
156+
const Hero = function(attributes) {
157+
Humanoid.call(this, attributes);
158+
}
159+
160+
Hero.prototype = Object.create(Humanoid.prototype);
161+
Hero.prototype.attack = function(target) {
162+
if(this.healthPoints > 0) {
163+
if(target.healthPoints > 0) {
164+
let damage = (Math.round(Math.random() * 10));
165+
target.healthPoints = target.healthPoints - damage;
166+
if (target.healthPoints > 0) {
167+
console.log(`${this.name} attacked ${target.name} for ${damage} points! ${target.name} now has ${target.healthPoints} health`)
168+
} else {
169+
console.log(`${this.name} attacked ${target.name} for ${damage} points and destroyed ${target.name}!`)
170+
}
171+
} else {
172+
console.log(`${this.name} kicked ${target.name}'s dead body`)
173+
}
174+
} else {
175+
console.log(`${this.name}'s body twitched`)
176+
}
177+
}
178+
179+
//Villain boi
180+
const Villain = function(attributes) {
181+
Humanoid.call(this, attributes);
182+
}
183+
184+
Villain.prototype = Object.create(Humanoid.prototype);
185+
Villain.prototype.attack = function(target) {
186+
if(this.healthPoints > 0) {
187+
if(target.healthPoints > 0) {
188+
let damage = (Math.round(Math.random() * 10));
189+
target.healthPoints = target.healthPoints - damage;
190+
if (target.healthPoints > 0) {
191+
console.log(`${this.name} attacked ${target.name} for ${damage} points! ${target.name} now has ${target.healthPoints} health`)
192+
} else {
193+
console.log(`${this.name} attacked ${target.name} for ${damage} points and destroyed ${target.name}!`)
194+
}
195+
} else {
196+
console.log(`${this.name} kicked ${target.name}'s dead body`)
197+
}
198+
} else {
199+
console.log(`${this.name}'s body twitched`)
200+
}
201+
}
202+
203+
204+
205+
206+
207+
208+
const aragorn = new Hero({
209+
createdAt: new Date(),
210+
dimensions: {
211+
length: 1,
212+
width: 1,
213+
height: 2,
214+
},
215+
healthPoints: 20,
216+
name: 'Aragorn',
217+
team: 'Ranger',
218+
weapons: [
219+
'Andúril',
220+
],
221+
language: 'The laguage of man',
222+
});
223+
224+
const lurtz = new Villain({
225+
createdAt: new Date(),
226+
dimensions: {
227+
length: 1,
228+
width: 1,
229+
height: 2,
230+
},
231+
healthPoints: 20,
232+
name: 'Lurtz',
233+
team: 'Uruk-hai',
234+
weapons: [
235+
'Bow',
236+
],
237+
language: 'black speech',
238+
});
239+
240+
lurtz.attack(aragorn);
241+
aragorn.attack(lurtz);
242+
lurtz.attack(aragorn);
243+
aragorn.attack(lurtz);
244+
lurtz.attack(aragorn);
245+
aragorn.attack(lurtz);
246+
lurtz.attack(aragorn);
247+
aragorn.attack(lurtz);
248+
lurtz.attack(aragorn);
249+
aragorn.attack(lurtz);
250+
lurtz.attack(aragorn);
251+
aragorn.attack(lurtz);
252+

‎assignments/this.js‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
/* The for principles of "this";
22
* in your own words. explain the four principle for the "this" keyword below.
33
*
4-
* 1.
5-
* 2.
6-
* 3.
7-
* 4.
4+
* 1. window binding describes a trait of this where if you dont say where you want this to
5+
go it will automtically revert to the window or the global scope depending on whether or
6+
not it is pointing at anything
7+
8+
* 2. implicit binding deals with objects and allows this to reach into it's parent object to
9+
find the vale of what it's referencing.
10+
11+
* 3. explicit binding is similar to implicit in that it is grabbing something within itself
12+
except that it is grabbing paramaters passed into the function it exists in instead og keys in
13+
its object
14+
15+
* 4. new binding refers to using the this keyword in object constructors and it's used to
16+
create key value pairs within the function that can be assigned to new objects based on the
17+
parameters passed into the function
818
*
919
* write out a code example of each explanation above
1020
*/
@@ -13,14 +23,60 @@
1323

1424
// code example for Window Binding
1525

26+
const name = {
27+
firstName: 'Jacob',
28+
lastName: 'Angulo'
29+
}
30+
31+
console.log(name.firstName + ' ' + name.lastName)
32+
1633
// Principle 2
1734

1835
// code example for Implicit Binding
1936

37+
const me = {
38+
firstName: 'Jacob',
39+
lastName: 'Angulo',
40+
speak: function() {
41+
console.log(this.firstName + ' ' + this.lastName)
42+
}
43+
}
44+
45+
me.speak();
46+
2047
// Principle 3
2148

2249
// code example for New Binding
2350

51+
const NameConstuctor = function(param) {
52+
this.firstName = param.firstName;
53+
this.lastName = param.lastName
54+
}
55+
56+
NameConstuctor.prototype.consoleFullName = function() {
57+
console.log(`${this.firstName} ${this.lastName}`)
58+
}
59+
60+
const miNombre = new NameConstuctor({
61+
firstName: 'Jacob',
62+
lastName: 'Angulo'
63+
});
64+
65+
miNombre.consoleFullName();
66+
2467
// Principle 4
2568

26-
// code example for Explicit Binding
69+
// code example for Explicit Binding
70+
71+
function sayName() {
72+
console.log(this.firstName + ' ' + this.lastName)
73+
}
74+
75+
const mePt2 = {
76+
firstName: 'Jacob',
77+
lastName: 'Angulo'
78+
};
79+
80+
const myNameAgain = sayName.bind(mePt2);
81+
82+
myNameAgain();

0 commit comments

Comments
 (0)