From e9e2d436b55dd00fc8c16b006a9b10e9cb187dd2 Mon Sep 17 00:00:00 2001 From: Darren J Carrillo Date: Thu, 22 Aug 2019 12:18:56 -0700 Subject: [PATCH 1/4] Prototype Refactor File Finished --- assignments/prototype-refactor.js | 181 ++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index 91424c9fa..ceec2b82c 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -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 ' 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 ' offers a greeting in .' + * 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! From 107b5cc1123e31d0dfff98eb1c337d1a6226b1a6 Mon Sep 17 00:00:00 2001 From: Darren J Carrillo Date: Thu, 22 Aug 2019 14:28:05 -0700 Subject: [PATCH 2/4] Almost done. Trying to figure out how to pass subject string as an argument --- assignments/lambda-classes.js | 150 ++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 71acfca0e..4d144380c 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -1 +1,151 @@ // CODE here for your Lambda Classes + +class Person { + constructor(personAttributes) { + this.newName = personAttributes.name; + this.newAge = personAttributes.age; + this.newLocation = personAttributes.location; + } + speak() { + return `Hello my name is ${this.newName}, I am from ${this.newLocation}`; + } +} + +class Instructor extends Person { + constructor(instructorAttributes) { + super(instructorAttributes); + this.newSpecialty = instructorAttributes.specialty; + this.newFavLanguage = instructorAttributes.favLanguage; + this.newCatchPhrase = instructorAttributes.catchPhrase; + } + demo() { + return `Today we are learning about ${this.newFavSubjects}`; + } + grade() { + return `${this.name} receives a perfect score on the ${this.subject}}`; + } +} + +class Students extends Person { + constructor(studentsAttributes) { + super(studentsAttributes); + this.newPreviousBackground = studentsAttributes.previousBackground; + this.newClassName = studentsAttributes.className; + this.newFavSubjects = studentsAttributes.favSubjects; + } + listsSubjects() { + return `${this.newFavSubjects}`; + } + PRAssignment() { + return `${this.newName} has submitted a PR for ${this.subject}`; + } + sprintChallenge() { + return `${this.newName} has begun sprint challenge on ${this.subject}`; + } +} + +class ProjectManager extends Instructor { + constructor(pmAttributes) { + super(pmAttributes); + this.newGradClassName = pmAttributes.gradClassName; + this.newFavInstructor = pmAttributes.favInstructor; + } + standUp() { + return `${this.NewName} annouces to ${ + this.channel + }, at @channel standy times!`; + } + debugsCode() { + return `${this.newName} debugs ${student.name}'s code on the ${ + this.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()); +console.log(madQueen.demo()); +console.log(eddard.demo()); +console.log(madQueen.grade()); + +// Student +console.log(arya.listsSubjects()); +console.log(joffrey.listsSubjects()); +console.log(arya.PRAssignment()); +console.log(joffrey.sprintChallenge()); + +// Project Managers +console.log(tyrion.standUp()); +console.log(varys.debugsCode()); From 5afd3ceead2b95c0b8166de9e4818a455f279380 Mon Sep 17 00:00:00 2001 From: Darren J Carrillo Date: Thu, 22 Aug 2019 15:48:48 -0700 Subject: [PATCH 3/4] Still trying to figure out the problem --- assignments/lambda-classes.js | 40 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 4d144380c..43e5ab722 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -2,9 +2,9 @@ class Person { constructor(personAttributes) { - this.newName = personAttributes.name; - this.newAge = personAttributes.age; - this.newLocation = personAttributes.location; + this.name = personAttributes.name; + this.age = personAttributes.age; + this.location = personAttributes.location; } speak() { return `Hello my name is ${this.newName}, I am from ${this.newLocation}`; @@ -14,41 +14,41 @@ class Person { class Instructor extends Person { constructor(instructorAttributes) { super(instructorAttributes); - this.newSpecialty = instructorAttributes.specialty; - this.newFavLanguage = instructorAttributes.favLanguage; - this.newCatchPhrase = instructorAttributes.catchPhrase; + this.specialty = instructorAttributes.specialty; + this.favLanguage = instructorAttributes.favLanguage; + this.catchPhrase = instructorAttributes.catchPhrase; } - demo() { - return `Today we are learning about ${this.newFavSubjects}`; + demo(subject) { + return `Today we are learning about ${subject}`; } - grade() { - return `${this.name} receives a perfect score on the ${this.subject}}`; + grade(students, subject) { + return `${student.name} receives a perfect score on the ${subject}`; } } class Students extends Person { constructor(studentsAttributes) { super(studentsAttributes); - this.newPreviousBackground = studentsAttributes.previousBackground; - this.newClassName = studentsAttributes.className; - this.newFavSubjects = studentsAttributes.favSubjects; + this.previousBackground = studentsAttributes.previousBackground; + this.className = studentsAttributes.className; + this.favSubjects = studentsAttributes.favSubjects; } listsSubjects() { - return `${this.newFavSubjects}`; + return `${this.favSubjects}`; } PRAssignment() { - return `${this.newName} has submitted a PR for ${this.subject}`; + return `${this.name} has submitted a PR for ${this.subject}`; } sprintChallenge() { - return `${this.newName} has begun sprint challenge on ${this.subject}`; + return `${this.name} has begun sprint challenge on ${this.subject}`; } } class ProjectManager extends Instructor { constructor(pmAttributes) { super(pmAttributes); - this.newGradClassName = pmAttributes.gradClassName; - this.newFavInstructor = pmAttributes.favInstructor; + this.gradClassName = pmAttributes.gradClassName; + this.favInstructor = pmAttributes.favInstructor; } standUp() { return `${this.NewName} annouces to ${ @@ -56,9 +56,7 @@ class ProjectManager extends Instructor { }, at @channel standy times!`; } debugsCode() { - return `${this.newName} debugs ${student.name}'s code on the ${ - this.subject - }`; + return `${this.newName} debugs ${arya.name}'s code on the ${this.subject}`; } } From d704c34dc63860ffaa17643e755ed8b3ef08bab5 Mon Sep 17 00:00:00 2001 From: Darren J Carrillo Date: Thu, 22 Aug 2019 22:58:56 -0700 Subject: [PATCH 4/4] Finnaly Done! Took Forever! --- assignments/lambda-classes.js | 42 +++++++++++++++++------------------ 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 43e5ab722..214bd6720 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -7,7 +7,7 @@ class Person { this.location = personAttributes.location; } speak() { - return `Hello my name is ${this.newName}, I am from ${this.newLocation}`; + return `Hello my name is ${this.name}, I am from ${this.location}`; } } @@ -19,10 +19,10 @@ class Instructor extends Person { this.catchPhrase = instructorAttributes.catchPhrase; } demo(subject) { - return `Today we are learning about ${subject}`; + console.log(`${this.name}: Today we are learning about ${subject}`); } - grade(students, subject) { - return `${student.name} receives a perfect score on the ${subject}`; + grade(student, subject) { + return `${student} receives a perfect score on the ${subject}`; } } @@ -36,11 +36,11 @@ class Students extends Person { listsSubjects() { return `${this.favSubjects}`; } - PRAssignment() { - return `${this.name} has submitted a PR for ${this.subject}`; + PRAssignment(subject) { + return `${this.name} has submitted a PR for ${subject}`; } - sprintChallenge() { - return `${this.name} has begun sprint challenge on ${this.subject}`; + sprintChallenge(subject) { + return `${this.name} has begun sprint challenge on ${subject}`; } } @@ -50,13 +50,11 @@ class ProjectManager extends Instructor { this.gradClassName = pmAttributes.gradClassName; this.favInstructor = pmAttributes.favInstructor; } - standUp() { - return `${this.NewName} annouces to ${ - this.channel - }, at @channel standy times!`; + standUp(slackChannel) { + return `${this.name} annouces to ${slackChannel} at @channel standy times!`; } - debugsCode() { - return `${this.newName} debugs ${arya.name}'s code on the ${this.subject}`; + debugsCode(student, subject) { + return `${this.name} debugs ${student}'s code on the ${subject}`; } } @@ -133,17 +131,17 @@ const varys = new ProjectManager({ console.log(eddard.speak()); console.log(jon.speak()); console.log(madQueen.speak()); -console.log(jon.demo()); -console.log(madQueen.demo()); -console.log(eddard.demo()); -console.log(madQueen.grade()); +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()); -console.log(joffrey.sprintChallenge()); +console.log(arya.PRAssignment("React")); +console.log(joffrey.sprintChallenge("Science")); // Project Managers -console.log(tyrion.standUp()); -console.log(varys.debugsCode()); +console.log(tyrion.standUp("WEB 23")); +console.log(varys.debugsCode("Jofrey", "JavaScript"));