Skip to content

Commit 543408a

Browse files
prototype.js stretch goals
1 parent e5cbb1b commit 543408a

2 files changed

Lines changed: 104 additions & 2 deletions

File tree

assignments/prototypes.js

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,92 @@ Humanoid.prototype.greet = function (){
140140
// Stretch task:
141141
// * Create Villian and Hero constructor functions that inherit from the Humanoid constructor function.
142142
// * Give the Hero and Villians 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;
143-
// * Create two new objects, one a villian and one a hero and fight it out with methods!
143+
// * Create two new objects, one a villian and one a hero and fight it out with methods!
144+
145+
function Villian(villianAttributes){
146+
Humanoid.call(this, villianAttributes);
147+
this.attack = villianAttributes.attack;
148+
this.type = villianAttributes.type;
149+
}
150+
151+
function Hero(heroAttributes){
152+
Humanoid.call(this, heroAttributes);
153+
this.attack = heroAttributes.attack;
154+
this.type = heroAttributes.type;
155+
}
156+
157+
Villian.prototype = Object.create(Humanoid.prototype);
158+
Hero.prototype = Object.create(Humanoid.prototype);
159+
160+
Villian.prototype.destroy = function () {
161+
return `${this.name} is defeated, and the town is saved! \n You Win!`;
162+
}
163+
164+
Hero.prototype.destroy = function () {
165+
return `You failed to save the town, ${this.name} has died. \n Game Over!`;
166+
}
167+
168+
Humanoid.prototype.fight = function (hero) {
169+
if(hero.type === this.type) {
170+
return `${this.name} refuses to engage.`
171+
}
172+
while(hero.hp > 0 && this.hp > 0) {
173+
random = Math.random();
174+
if(random > .5){
175+
hero.hp = hero.hp - this.attack;
176+
if (hero.hp > 0){
177+
console.log(`${hero.name} takes ${this.attack} damage. Hero hp ${hero.hp}`)
178+
} else {
179+
return (hero.destroy());
180+
}
181+
}
182+
if(random < .5) {
183+
this.hp = this.hp - hero.attack;
184+
if (this.hp > 0){
185+
console.log(`${this.name} takes ${hero.attack} damage. Villian hp ${this.hp}`)
186+
} else {
187+
return (this.destroy());
188+
}
189+
}
190+
}
191+
}
192+
193+
const darknessFalls = new Villian({
194+
createdAt: new Date(),
195+
dimensions: {
196+
length: 10,
197+
width: 20,
198+
height: 400,
199+
},
200+
hp: 66,
201+
name: 'Lucifer',
202+
faction: 'Underworld',
203+
weapons: [
204+
'Hell Hounds',
205+
'Sicle',
206+
],
207+
language: 'Demon',
208+
attack: 10,
209+
type: "villain",
210+
});
211+
212+
const Harmony = new Hero({
213+
createdAt: new Date(),
214+
dimensions: {
215+
length: 1,
216+
width: 2,
217+
height: 100,
218+
},
219+
hp: 50,
220+
name: 'Lance',
221+
faction: 'Heavens',
222+
weapons: [
223+
'Trumpet of Truth',
224+
'Staff',
225+
],
226+
language: 'Angel',
227+
attack: 8,
228+
type: "hero",
229+
});
230+
231+
console.log(darknessFalls.fight(Harmony));

assignments/this.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,18 @@ const Teddy = new Dog("Yorkie");
7979
const Snuggles = new Cat("Tabby");
8080

8181
console.log(Teddy);
82-
console.log(Snuggles);
82+
console.log(Snuggles);
83+
84+
const Turtle = {
85+
name: "Franklin",
86+
}
87+
88+
const actions = ["walk", "sleep", "eat", "poop"];
89+
90+
function printActions(walk, sleep, eat, poop){
91+
console.log(`Hi, my name is ${this.name}. I like to ${walk}, ${sleep}, ${eat}, and ${poop}`);
92+
}
93+
94+
printActions.call(Turtle, ...actions);
95+
printActions.apply(Turtle, actions);
96+
const turtleMessage = printActions.bind(Turtle) ("walk", "sleep", "eat", "poop");

0 commit comments

Comments
 (0)