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

// Person
class Person {
constructor(attrib) {
this.name = attrib.name;
this.age = attrib.age;
this.location = attrib.location;
}
speak() {
console.log (`Hello. My name is ${this.name} and I am from ${this.location}`);
}
}

// Student
class Student extends Person {
constructor(studAttrib) {
super(studAttrib);
this.previousBackground = studAttrib.previousBackground;
this.className = studAttrib.className;
this.favSubjects = studAttrib.favSubjects;
}
listsSubjects() {
for (let i=0; i < this.favSubjects.length; i++)
console.log(this.favSubjects[i]);
}
PRAssignment(subject) {
console.log(`${this.name} has submitted a PR for ${subject}`);
}
sprintChallenge(subject) {
console.log(`${this.name} has begun sprint challenge on ${subject}`);
}

}

// Instructors
class Instructor extends Person {
constructor(instAttrib) {
super(instAttrib);
this.specialty = instAttrib.specialty;
this.favLanguage = instAttrib.favLanguage;
this.catchPhrase = instAttrib.catchPhrase;
}
demo(subjectStr) {
console.log (`Today we are learning about ${subjectStr}`);
}
grade(studentObj, subjectStr) {
console.log (`${studentObj.name} receives a perfect score on ${subjectStr}`);
}
}


// Project Managers
class ProjectManager extends Instructor {
constructor(pmAttrib) {
super(pmAttrib);
this.gradClassName = pmAttrib.gradClassName;
this.favInstructor = pmAttrib.favInstructor;
}
standUp(slackChannel) {
console.log(`${this.name} announces to ${slackChannel}, @channel standy times!`);
}
debugsCode(studentObj, subjectStr) {
console.log(`${this.name} debugs ${studentObj.name}'s code on ${subjectStr}`);
}
}

// ******************** Creating the objects (people) *******************
// Cast of Characters:
// Instructors: Mr. Garrison, Mr. Mackay
// Project Managers: Chef, Stan
// Students: Cartman, Kenny, Kyle
// ***********************************************************************

const mr_garrison = new Instructor({
name: "Mr. Garrison",
age: 47,
location: "South Park",
specialty: "Math",
favLanguage: "English",
catchPhrase: "There are no stupid questions; only stupid people."
});
const mr_mackay = new Instructor({
name: "Mr. Mackay",
age: 42,
location: "South Park",
specialty: "pharmaceuticals",
favLanguage: "Java",
catchPhrase: "Drugs are bad, mmmmkay?"

});
const chef = new ProjectManager({
name: "Chef",
age: 35,
location: "Chicago",
gradClassName: "Self-taught",
favInstructor: "Barry White"

});
const stan = new ProjectManager({
name: "Stan",
age: 15,
location: "South Park",
gradClassName: "WEB12",
favInstructor: "Mr. Garrison",
});
const cartman = new Student({
name: "Cartman",
age: 11,
location: "South Park",
previousBackground: "4th Grade",
className: "WEB30",
favSubjects: ["Cooking", "Eating", "Sleeping"],
});
const kenny = new Student({
name: "Kenny",
age: 13,
location: "South Park",
previousBackground: "Dressing Warm",
className: "WEB23",
favSubjects: ["Home Ec", "Speech and Debate"],
});
const kyle = new Student({
name: "Kyle",
age: 12,
location: "South Park",
previousBackground: "Playground activities",
className: "CS5",
favSubjects: ["Math"],
});



// ****************** Testing ******************
console.log("\n\n");

// 1 or 2 levels of function inheritance.
// Introductions all-around:
mr_garrison.speak();
mr_mackay.speak();
chef.speak();
stan.speak();
cartman.speak();
kenny.speak();
kyle.speak();

// Student functions
cartman.listsSubjects();
kenny.PRAssignment("JavaScript IV");
kyle.sprintChallenge("React");

// Instructor functions
mr_garrison.demo("College");
mr_mackay.grade(kyle, "HTML");

// Project Manager functions
chef.standUp("#Lunch");
stan.debugsCode(kenny, "ReactJS");

// Directly access attributes
console.log("Stan's favorite instructor is " + stan.favInstructor);
console.log("Mr. Mackay likes to say \"" + mr_mackay.catchPhrase + "\"");
console.log("Cartman will graduate with " + cartman.className + ". Maybe.");
136 changes: 133 additions & 3 deletions assignments/prototype-refactor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,139 @@
/*

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

/*
=== 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 (attributes) {
this.createdAt = attributes.createdAt,
this.name = attributes.name,
this.dimensions = attributes.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 (statsAttrib) {
super (statsAttrib);
this.healthPoints = statsAttrib.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(humanAttrib) {
super (humanAttrib);
this.team = humanAttrib.team,
this.weapons = humanAttrib.weapons,
this.language = humanAttrib.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 your 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!