From f1695ace0fc17d2eec850c11e1bd7fa9e60781bd Mon Sep 17 00:00:00 2001 From: jarrodskahill Date: Thu, 17 Oct 2019 12:01:53 -0700 Subject: [PATCH 1/4] added code from yesterday --- assignments/prototype-refactor.js | 142 ++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index 91424c9fa..35755da73 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -7,3 +7,145 @@ 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.` +*/ + function GameObject (mainat) { + this.createdAt = mainat.createdAt; + this.dimensions = mainat.dimensions; + this.name = mainat.name; + }; + GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game.`; + }; + + + /* + === CharacterStats === + * healthPoints + * takeDamage() // prototype method -> returns the string ' took damage.' + * should inherit destroy() from GameObject's prototype + */ + function CharacterStats(mainat) { + GameObject.call(this, mainat); + this.healthPoints = mainat.healthPoints; + this.name = mainat.name; + } + CharacterStats.prototype = Object.create(GameObject.prototype); + + CharacterStats.prototype.takeDamage = function() { + 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 + */ + function Humanoid(mainat) { + CharacterStats.call(this, mainat); + this.team = mainat.team; + this.weapons = mainat.weapons; + this.language = mainat.language; + } + + Humanoid.prototype = Object.create(CharacterStats.prototype); + + Humanoid.prototype.greet = function() { + 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 29c421936069a95ab8c8aec96a3817be571201f7 Mon Sep 17 00:00:00 2001 From: jarrodskahill Date: Thu, 17 Oct 2019 12:17:47 -0700 Subject: [PATCH 2/4] converted old prototypes to ES6 with use of class and extends --- assignments/prototype-refactor.js | 43 +++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index 35755da73..13c1b6530 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -25,14 +25,19 @@ Prototype Refactor * dimensions (These represent the character's size in the video game) * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ - function GameObject (mainat) { + class GameObject { + constructor(mainat){ this.createdAt = mainat.createdAt; this.dimensions = mainat.dimensions; this.name = mainat.name; + } + + + destroy() { + return `${this.name} was removed from the game.`; + } }; - GameObject.prototype.destroy = function() { - return `${this.name} was removed from the game.`; - }; + /* @@ -41,16 +46,17 @@ Prototype Refactor * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ - function CharacterStats(mainat) { - GameObject.call(this, mainat); + class CharacterStats extends GameObject { + constructor(mainat){ + super(mainat); this.healthPoints = mainat.healthPoints; - this.name = mainat.name; - } - CharacterStats.prototype = Object.create(GameObject.prototype); - - CharacterStats.prototype.takeDamage = function() { + } + takeDamage() { return `${this.name} took damage`; }; +} + + /* === Humanoid (Having an appearance or character resembling that of a human.) === @@ -61,18 +67,21 @@ Prototype Refactor * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - function Humanoid(mainat) { - CharacterStats.call(this, mainat); + class Humanoid extends CharacterStats { + constructor(mainat){ + super(mainat); this.team = mainat.team; this.weapons = mainat.weapons; this.language = mainat.language; + } + greet() { + return `${this.name} offers a greeting in ${this.language}.`; + }; + } - Humanoid.prototype = Object.create(CharacterStats.prototype); - Humanoid.prototype.greet = function() { - return `${this.name} offers a greeting in ${this.language}.`; - }; + /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid From 4c51f3782e583b6687824b94d068d4b6571b73b1 Mon Sep 17 00:00:00 2001 From: jarrodskahill Date: Thu, 17 Oct 2019 12:28:23 -0700 Subject: [PATCH 3/4] added person class(base) --- assignments/lambda-classes.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 71acfca0e..15bc2306d 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -1 +1,12 @@ // CODE here for your Lambda Classes +class Person { + constructor(info) { + this.name = info.name; + this.age = info.age; + this.location = info.location; + } + speak () { + return `Hello, my name is ${this.name}, I am from ${this.location} ` + } + +} \ No newline at end of file From 4d01549ff8c96842e77732f69ea96f38d134db25 Mon Sep 17 00:00:00 2001 From: jarrodskahill Date: Thu, 17 Oct 2019 15:34:00 -0700 Subject: [PATCH 4/4] finished lambda-classes --- assignments/lambda-classes.js | 144 +++++++++++++++++++++++++++++++++- 1 file changed, 143 insertions(+), 1 deletion(-) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 15bc2306d..62ff83dee 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -9,4 +9,146 @@ class Person { return `Hello, my name is ${this.name}, I am from ${this.location} ` } -} \ No newline at end of file +} + +class Instructor extends Person { + constructor(info) { + super(info); + this.specialty = info.specialty; + this.favLanguage = info.favLanguage; + this.catchPhrase = info.catchPhrase; + } + demo (subject) { + return `Today we are learning about ${subject}`; + }; + + grade (name, subject) { + return `${name} recieves a perfect score on the ${subject} test` + } + +} + +class Student extends Person { + constructor(info) { + super(info); + this.previousBackground = info.previousBackground; + this.classname = info.classname; + this.favSubjects = info.favSubjects; + } + listsSubjects () { + this.favSubjects.forEach(function(subject){ + return console.log(subject); + }); + } + + PRAssignment (subject2) { + return `${this.name} has submitted a PR for ${subject2}.`; + } + + sprintChallenge(subject3) { + return `${this.name} has begun sprint challenge on ${subject3}.`; + } + + grade(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return `${this.name}'s grade ` + Math.round(Math.random() * (max - min) + min); + } +} + +class ProjectManagers extends Instructor { + constructor(info) { + super(info); + this.gradClassName = info.gradClassName; + this.favInstructor = info.favInstructor; + } + + standUp(name, channel) { + return `${name} announces to ${channel}, @channel standy times!`; + } + + debugsCode (name, studobj) { + return `${name} debugs ${this.name}'s code on ${studobj}.`; + } +} + +const rodger = new Instructor({ + name: 'Rodger', + location: 'New York', + age: 26, + favLanguage: 'JavaScript', + specialty: 'Front-end', + catchPhrase: `Milirock` + }); + + const kevin = new Instructor({ + name: 'Kevin', + location: 'New Jeresy', + age: 33, + favLanguage: 'JavaScript', + specialty: 'back-end', + catchPhrase: `Peace of cake` + }); + + const karen = new Student({ + name: 'Karen', + location: 'texas', + age: 30, + favLanguage: 'C#', + specialty: 'back-end', + gradClassName: `CS1`, + className: `CS132`, + favSubjects: ['math ','science ', 'history '], + previousBackground: `Dishwasher`, + }); + + const kristen = new Student({ + name: 'Kristen', + location: 'Greenbay', + age: 19, + favLanguage: 'CSS', + specialty: 'front-end', + gradClassName: `CS7`, + favInstructor: `Bill`, + className: `CS135`, + previousBackground: `Dishwasher`, + favSubjects: ['science ', 'social studies ', 'computer science '] + }); + + const dani = new ProjectManagers({ + name: 'Dani', + location: 'Charlotte', + age: 41, + favLanguage: 'C++', + specialty: 'back-end', + catchPhrase: `Just do it`, + gradClassName: `CS9`, + favInstructor: `Tommy`, + className: `CS138`, + favSubjects: `CSS`, + previousBackground: 'sewing' + }); + + const diane = new ProjectManagers({ + name: 'Diane', + location: 'Vegas', + age: 35, + favLanguage: 'C#', + specialty: 'back-end', + catchPhrase: `That was easy`, + gradClassName: `CS9`, + favInstructor: `Hilary`, + className: `CS138`, + favSubjects: `C#`, + previousBackground: 'stripping' + }); + + console.log(diane.speak()); + kristen.listsSubjects(); + console.log(kristen.PRAssignment(`CSS`)); + console.log(karen.sprintChallenge(`Javascript`)); + console.log(kevin.demo(`math`)); + console.log(rodger.grade(`Karen`, `science`)); + console.log(diane.standUp(`Diane`, `WEB25`)); + console.log(dani.debugsCode(`Jonathan`, `Javascript`)); + console.log(karen.grade(1, 100)) \ No newline at end of file