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
161 changes: 161 additions & 0 deletions assignments/lambda-classes.js
Original file line number Diff line number Diff line change
@@ -1 +1,162 @@
// CODE here for your Lambda Classes


class Person{
constructor(data){
this.name = data.name;
this.location = data.location;
this.age = data.age;
};
speak(){
return `Hello my name is ${this.name}, I am from ${this.location}.`;
};
};

class Students extends Person{
constructor(data){
super(data);
this.previousBackground = data.previousBackground;
this.className = data.className;
this.favSubjects = data.favSubjects;
this.grade = Math.round(Math.random() * 99) + 1;
};
listSubjects(){
for(let i = 0; i < this.favSubjects.length; ++ i){
console.log(`${i+1}) ${this.favSubjects[i]}`);
}
};
PRAssignment(subject){
return `${this.name} has submitted a PR for ${subject}`;
};
sprintChallenge(subject){
return `${this.name} has begun sprint challenge on ${subject}`;
};
graduate(){
if (this.grade >= 70){
return `${this.name} has successfully passed and is ready for graduation!`;
}else{
return `${this.name} is not ready to graduate and should complete more assignments.`;
}
}
};

class Instructors extends Person{
constructor(data){
super(data);
this.favLanguage = data.favLanguage;
this.specialty = data.specialty;
this.catchPhrase = data.catchPhrase;
};
demo(subject){
return `Today we are learning about ${subject}`;
};
grade(student, subject){
let nScore = Math.round(Math.random() * 40)-20;
student.grade += nScore;
return `${student.name} receives a ${nScore} on ${subject}`;
};
};

class ProjectManagers extends Instructors{
constructor(data){
super(data);
this.gradClassName = data.gradClassName;
this.favInstructor = data.favInstructor;
}
standUp(slackChannel){
return `${this.name} announces to ${slackChannel}, @channel standy times!`;
};
debugCode(student, subject){
return `${this.name} debugs ${student.name}'s code on ${subject}`;
};
};

const AverageJoe = new Person({
name: "Joe",
age: 30,
location: "USA"
})

const Anthony = new Students({
name: "Anthony",
age: 26,
location: "KY",
previousBackground: "Developer",
className: "WEB21",
favSubjects: ['JavaScript', 'Backend']
});

const DanLevy = new Instructors({
name: "Dan Levy",
age: 100,
location: "United States",
previousBackground: "Student at LS",
className: "WEB21",
favSubjects: ['Cats', 'Cat', 'Kitler'],
favLanguage: 'CSS',
specialty: 'Teaching',
catchPhrase: 'Good morning'
});

const ChristianI = new ProjectManagers({
name: "Christian",
age: 100,
location: "Internet",
previousBackground: "Student at LS",
className: "WEB21",
favSubjects: ['Front end', 'Back end', 'Full stack'],
favLanguage: 'English',
specialty: 'Being awesome',
catchPhrase: 'Good afternoon',
gradClassName: 'WEB18',
favInstructor: 'Josh?'
})

console.log("################");
console.log("#### Person ####");
console.log("################");
console.log(AverageJoe.speak());

console.log("#################");
console.log("#### Student ####");
console.log("#################");
console.log(Anthony.speak());
Anthony.listSubjects();
console.log(Anthony.PRAssignment(Anthony.favSubjects[0]));
console.log(Anthony.sprintChallenge(Anthony.favSubjects[0]));

console.log("####################");
console.log("#### Instructor ####");
console.log("####################");
console.log(DanLevy.speak());
console.log(DanLevy.demo('JavaScript'));
console.log(DanLevy.grade(Anthony, Anthony.favSubjects[0]));

console.log("#########################");
console.log("#### Project Manager ####");
console.log("#########################");
console.log(ChristianI.speak());
console.log(ChristianI.demo('JavaScript'));
console.log(ChristianI.grade(Anthony, Anthony.favSubjects[1]));
console.log(ChristianI.standUp('#Help21'));
console.log(ChristianI.debugCode(Anthony, Anthony.favSubjects[1]));

console.log("#####################");
console.log("### Stretch Goals ###");
console.log("#####################");
console.log(Anthony.graduate());
let nTimes = 0;
while (Anthony.grade < 70 && nTimes < 8){
WorkHard();
console.log(Anthony.graduate());
nTimes++;
}
if (Anthony.grade < 70){ // I added this just so the console would never get flooded. No one should give up on their goals!
console.log(`${Anthony.name} has not been successful in graduating and should persue a different career`);
}


function WorkHard(){ // This isn't necessary as these 2 lines could be put into the While loop. I just wanted to show I know how to do it
console.log(ChristianI.grade(Anthony, Anthony.favSubjects[1]));
console.log(DanLevy.grade(Anthony, Anthony.favSubjects[0]));
}
188 changes: 182 additions & 6 deletions assignments/prototype-refactor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,185 @@
/*
/*
Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.

Prototype Refactor

1. Copy and paste your code or the solution from yesterday

2. Your goal is to refactor all of this code to use ES6 Classes. The console.log() statements should still return what is expected of them.
In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.

At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions.

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

/*
=== GameObject ===
* createdAt
* name
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/
class GameObject {
constructor(data){
this.createdAt = data.createdAt;
this.name = data.name;
this.dimensions = data.dimensions;
};
destroy(){
return this.name + " was removed from the game.";
};
};

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
class CharacterStats extends GameObject{
constructor(data){
super(data);
this.healthPoints = data.healthPoints;
}
takeDamage() {
return this.name + " took damage.";
};
};

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
* weapons
* language
* greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

class Humanoid extends CharacterStats{
constructor(data){
super(data);
this.team = data.team;
this.weapons = data.weapons;
this.language = data.language;
};
greet() {
return this.name + " offers a greeting in " + this.language;
};
};

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

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


// 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!

const Villain = new Humanoid({
createdAt: new Date(),
healthPoints: 20,
name: 'The villain',
weapons: ['Bear hands', 'Skeleton army'],
language: 'Russian'
});

const Hero = new Humanoid({
createdAt: new Date(),
healthPoints: 20,
name: 'Hero',
weapons: ['Bare hands', 'Vodka'],
language: 'English'
});

Humanoid.prototype.attackWith = function(weapon, target){
if (this.healthPoints <= 0) return;
target.healthPoints-=5;
console.log(this.name + " attacks " + target.name + " with his " + weapon);
target.checkHealth();
};

Humanoid.prototype.checkHealth = function(){
if (this.healthPoints <= 0){
console.log(this.name + " has fallen.");
}
}

while (Hero.healthPoints > 0 && Villain.healthPoints > 0){
if (Villain.healthPoints === 5){
Hero.attackWith(Hero.weapons[1], Villain);
}else{
Hero.attackWith(Hero.weapons[0], Villain);
}
Villain.attackWith(Villain.weapons[Math.round(Math.random())], Hero);
}