Skip to content

JavaScript IV #665

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
146 changes: 146 additions & 0 deletions assignments/lambda-classes.js
Original file line number Diff line number Diff line change
@@ -1 +1,147 @@
// CODE here for your Lambda Classes

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

class Instructor extends Person {
constructor(instructorAttributes) {
super(instructorAttributes);
this.specialty = instructorAttributes.specialty;
this.favLanguage = instructorAttributes.favLanguage;
this.catchPhrase = instructorAttributes.catchPhrase;
}
demo(subject) {
console.log(`${this.name}: Today we are learning about ${subject}`);
}
grade(student, subject) {
return `${student} receives a perfect score on the ${subject}`;
}
}

class Students extends Person {
constructor(studentsAttributes) {
super(studentsAttributes);
this.previousBackground = studentsAttributes.previousBackground;
this.className = studentsAttributes.className;
this.favSubjects = studentsAttributes.favSubjects;
}
listsSubjects() {
return `${this.favSubjects}`;
}
PRAssignment(subject) {
return `${this.name} has submitted a PR for ${subject}`;
}
sprintChallenge(subject) {
return `${this.name} has begun sprint challenge on ${subject}`;
}
}

class ProjectManager extends Instructor {
constructor(pmAttributes) {
super(pmAttributes);
this.gradClassName = pmAttributes.gradClassName;
this.favInstructor = pmAttributes.favInstructor;
}
standUp(slackChannel) {
return `${this.name} annouces to ${slackChannel} at @channel standy times!`;
}
debugsCode(student, subject) {
return `${this.name} debugs ${student}'s code on the ${subject}`;
}
}

//*********** INSTRUCTORS ***********//

const eddard = new Instructor({
name: "Eddard Stark",
location: "Winterfell",
age: 45,
favLanguage: "Common Tongue",
specialty: "Sword Fighting",
catchPhrase: `The man who passes the sentence should swing the sword.`
});

const jon = new Instructor({
name: "Jon Snow",
location: "Castle Black",
age: 23,
favLanguage: "Common Tongue",
specialty: "Sword Fighting",
catchPhrase: `Winter is coming!`
});

const madQueen = new Instructor({
name: "Daenarys Targeryan",
location: "Essos",
age: 22,
favLanguage: "Dothraki & Valerian",
specialty: "Riding Dragons",
catchPhrase: "The Iron Throne is mine! Dracarys rawr!"
});

//*********** STUDENTS ***********//
const arya = new Students({
name: "Arya Stark",
location: "Winterfell",
age: 15,
className: "Web 23",
favSubjects: ["HTML", "CSS", "JavaScript", "Phython"],
specialty: "Faceless Assassin",
catchPhrase: `A girl has no name!`
});

const joffrey = new Students({
name: "Joffrey Baratheon",
location: "Kings Landing",
age: 15,
className: "Web 23",
favSubjects: ["Ruby ", "C++ ", "PHP ", "C#"],
specialty: "Being an ass",
catchPhrase:
'They say Stannis never smiles. I"ll give him a Red smile, from ear to ear'
});

//*********** PROJECT MANAGERS ***********//

const tyrion = new ProjectManager({
name: "Tyrion Lannister",
location: "Kings Landing",
age: 35,
gradClassName: "Blackwater University",
favInstructor: "Sir Bron"
});

const varys = new ProjectManager({
name: "Lord Varys",
location: "Kings Landing",
age: 48,
gradClassName: "Wisperers University",
favInstructor: "Mad King"
});

// Instructors
console.log(eddard.speak());
console.log(jon.speak());
console.log(madQueen.speak());
console.log(jon.demo("White Walkers"));
console.log(madQueen.demo("Dragons"));
console.log(eddard.demo("Honor"));
console.log(madQueen.grade("Arya", "99"));

// Student
console.log(arya.listsSubjects());
console.log(joffrey.listsSubjects());
console.log(arya.PRAssignment("React"));
console.log(joffrey.sprintChallenge("Science"));

// Project Managers
console.log(tyrion.standUp("WEB 23"));
console.log(varys.debugsCode("Jofrey", "JavaScript"));
181 changes: 181 additions & 0 deletions assignments/prototype-refactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,184 @@ 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.`
*/

// Making the constructor
// function GameObject(gameAttribute) {
// (this.createdAt = gameAttribute.createdAt),
// (this.name = gameAttribute.name),
// (this.dimensions = gameAttribute.dimensions);
// }

// // Prototype for this constructor
// GameObject.prototype.destroy = function() {
// return `${this.name} was removed from the game.`;
// };

// *************CONVERTED TO CLASSS ***************//
class GameObject {
constructor(gameAttribute) {
this.createdAt = gameAttribute.createdAt;
this.name = gameAttribute.name;
this.dimensions = gameAttribute.dimensions;
}
destroy() {
return `${this.name} was removed from the game.`;
}
} // closes the GameObject

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/

// // Making the constructor for CharacterStats
// function CharacterStats(charAttributes) {
// (this.healthPoints = charAttributes.healthPoints),
// GameObject.call(this, charAttributes);
// }

// // Inheritance
// CharacterStats.prototype = Object.create(GameObject.prototype);

// // Prototype for CharacterStats constructor
// CharacterStats.prototype.takeDamage = function() {
// return `${this.name} took damage.`;
// };

// *************CONVERTED TO CLASSS ***************//

class CharacterStats extends GameObject {
constructor(charAttributes) {
super(charAttributes);
this.healthPoints = charAttributes.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
*/

// Humanoid Constructor
// function Humanoid(humanoidAttributes) {
// (this.team = humanoidAttributes.team),
// (this.weapons = humanoidAttributes.weapons),
// (this.language = humanoidAttributes.language),
// CharacterStats.call(this, humanoidAttributes);
// }

// // Inheritance
// Humanoid.prototype = Object.create(CharacterStats.prototype);

// // Prototype for this constructor
// Humanoid.prototype.greet = function() {
// return `${this.name} offers a greeting in ${this.language}`;
// };

// *************CONVERTED TO CLASSS ***************//

class Humanoid extends CharacterStats {
constructor(humanoidAttributes) {
super(humanoidAttributes);
this.team = humanoidAttributes.team;
this.teapons = humanoidAttributes.weapons;
this.language = humanoidAttributes.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!