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

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

class Student extends Person {
constructor(profile) {
super(profile);
this.previousBackground = profile.previousBackground;
this.className = profile.className;
this.favSubjects = profile.favSubjects;
// STRETCH
this.grade = profile.grade;
}
listsSubjects() {
return `${this.favSubjects.forEach(subject => console.log(subject))}`;
}
PRAssignment(subject) {
return `${this.name} has submitted a PR for ${subject}`;
}
sprintChallenge(subject) {
return `${this.name} has begun sprint challenge on ${subject}`;
}
// STRETCH
graduateReady() {
return this.grade > 70
? `${this.name} is ready to graduate`
: `${this.name} is not ready to graduate`;
}
}

class Instructor extends Person {
constructor(attrs) {
super(attrs);
this.specialty = attrs.specialty;
this.favLanguage = attrs.favLanguage;
this.catchPhrase = attrs.catchPhrase;
}
demo(subject) {
return `Today we are learning about ${subject}`;
}
grade(student, subject) {
return `${student.name} receives a perfect score on ${subject}`;
}
// STRETCH
gradeUpdate(student) {
let random = Math.floor(Math.random() * 10 + 1);
let plusOrMinus = Math.random() < 0.5 ? -1 : 1;
random = random * plusOrMinus;
return `${this.name} updates ${
student.name
}'s grade to ${(student.grade += random)}`;
}
}

class TeamLead extends Instructor {
constructor(tlAttrs) {
super(tlAttrs);
this.gradClassName = tlAttrs.gradClassName;
this.favInstructor = tlAttrs.favInstructor;
}
standUp(channel) {
return `${this.name} announces to ${channel}, @channel standy times!`;
}
debugsCode(student, subject) {
return `${this.name} debugs ${student.name}'s code on ${subject}`;
}
}

const john = new Student({
name: 'John',
age: 42,
location: 'LA',
grade: 90,
previousBackground: 'CSUF',
className: 'WEBPT9',
favSubjects: ['Javascript', 'React', 'GraphQL']
});

console.log('============================================');
console.log(john.PRAssignment('Javascript'));
console.log(john.listsSubjects());
console.log(john.sprintChallenge('Javascript'));

const pace = new Instructor({
name: 'Pace',
location: 'Arizona',
age: 32,
specialty: 'Front-End',
favLanguage: 'Javascript',
catchPhrase: 'You guys are great!'
});

console.log('============================================');
console.log(pace.demo('Javascript'));
console.log(pace.grade(john, 'Javascript'));

const marshall = new TeamLead({
name: 'Marshall',
age: '27',
location: 'Seattle',
speciality: 'Front-End',
favLanguage: 'Javascript',
catchPhrase: "I'm proud of you"
});

console.log('============================================');
console.log(marshall.standUp('webpt9_marshall'));
console.log(marshall.debugsCode(john, 'Javascript'));

// Stretch Problem

console.log('===============STRETCH TEST=================');
console.log(`${john.name}'s grade is ${john.grade}`);

console.log(pace.gradeUpdate(john));
console.log(pace.gradeUpdate(john));
console.log(pace.gradeUpdate(john));
console.log(pace.gradeUpdate(john));
console.log(pace.gradeUpdate(john));
console.log(pace.gradeUpdate(john));

console.log(marshall.gradeUpdate(john));
console.log(marshall.gradeUpdate(john));
console.log(marshall.gradeUpdate(john));
console.log(marshall.gradeUpdate(john));
console.log(marshall.gradeUpdate(john));
console.log(marshall.gradeUpdate(john));

console.log(john.graduateReady());
233 changes: 232 additions & 1 deletion assignments/prototype-refactor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*

Prototype Refactor

Expand All @@ -7,3 +7,234 @@ Prototype Refactor
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.

*/

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

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(create) {
this.createdAt = create.createdAt;
this.name = create.name;
this.dimensions = create.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(stats) {
super(stats);
this.healthPoints = stats.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(human) {
super(human);
this.team = human.team;
this.weapons = human.weapons;
this.language = human.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.
*/

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!

class Hero extends Humanoid {
constructor(heroClass) {
super(heroClass);
this.armorType = heroClass.armorType;
this.special = heroClass.special;
}
attack() {
let attackDmg = 200;
let enemy = arthas.name;
let enemyHealth = arthas.healthPoints;
return () => {
enemyHealth = enemyHealth - attackDmg;
enemyHealth > 1
? console.log(
`${this.name} ${
this.special
}'s ${enemy} for ${attackDmg}. ${enemy} has ${enemyHealth} health points.`
)
: console.log(
`${this.name} ${
this.special
}'s ${enemy} and ${enemy} crumbles and falls dead.`
);
};
}
}

class Villain extends Humanoid {
constructor(villainClass) {
super(villainClass);
this.armorType = villainClass.armorType;
this.special = villainClass.special;
}
attack() {
let attackDmg = 200;
let enemy = thrall.name;
let enemyHealth = thrall.healthPoints;
return () => {
enemyHealth = enemyHealth - attackDmg;
enemyHealth > 1
? console.log(
`${this.name} ${
this.special
}'s ${enemy} for ${attackDmg}. ${enemy} has ${enemyHealth} health points.`
)
: console.log(
`${this.name} ${
this.special
}'s ${enemy} and ${enemy} crumbles and falls dead.`
);
};
}
}

const thrall = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 3,
height: 6
},
healthPoints: 1000,
name: 'Thrall',
team: 'Horde',
weapons: ['Axe'],
language: 'Orcish',
armorType: 'Plate',
special: 'Mortal Strike'
});

const arthas = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 7
},
healthPoints: 1000,
name: 'Arthas',
team: 'Alliance',
weapons: ['Sword'],
language: 'Common Tongue',
armorType: 'Plate',
special: 'Heart Strike'
});

let thrallAttack = thrall.attack();
let arthasAttack = arthas.attack();

thrallAttack(); // Thrall Mortal Strike's Arthas for 200. Arthas has 800 health points.
arthasAttack(); // Arthas Heart Strike's Thrall for 200. Thrall has 800 health points.
arthasAttack(); // Arthas Heart Strike's Thrall for 200. Thrall has 600 health points.
arthasAttack(); // Arthas Heart Strike's Thrall for 200. Thrall has 400 health points.
thrallAttack(); // Thrall Mortal Strike's Arthas for 200. Arthas has 600 health points.
thrallAttack(); // Thrall Mortal Strike's Arthas for 201. Arthas has 400 health points.
arthasAttack(); // Arthas Heart Strike's Thrall for 200. Thrall has 200 health points.
thrallAttack(); // Thrall Mortal Strike's Arthas for 200. Arthas has 200 health points.
thrallAttack(); // Thrall Mortal Strike's Arthas and Arthas crumbles and falls dead.