From d8df092f39a1af1b0931ee6a03b2cda693ce78b0 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 11 Oct 2019 22:15:42 -0400 Subject: [PATCH 1/3] JS-IV --- assignments/prototype-refactor.js | 152 ++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index 91424c9fa..6607a5838 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -7,3 +7,155 @@ 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. */ + + +// === GameObject === + +function GameObject(attributes){ + this.createdAt = attributes.createdAt; + this.name = attributes.name; + this.dimensions = attributes.dimensions; +} +GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game.`; +} + +// === CharacterStats === + +function CharacterStats(charAttributes) { + GameObject.call(this, charAttributes); + this.healthPoints = charAttributes.healthPoints; +} + +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.) === + +function Humanoid(humanAttributes) { + CharacterStats.call(this, humanAttributes); + this.team = humanAttributes.team; + this.weapons = humanAttributes.weapons; + this.language = humanAttributes.language; +} + +Humanoid.prototype = Object.create(CharacterStats.prototype); + +Humanoid.prototype.greet = function() { + return `${this.name} offers a greeting in ${this.language}.` +} + +// === Stretch task: === + +function Villain(villainAttributes) { + Humanoid.call(this, villainAttributes); + this.deception = villainAttributes.deception; + this.poison = villainAttributes.poison; +} + +Villain.prototype = Object.create(Humanoid.prototype); + +Villain.prototype.stabBack = function() { + return `Plot twist! ${this.name} has just deceived you!` +} + +function Hero (heroAttributes) { + Humanoid.call(this, heroAttributes); + this.truth = heroAttributes.truth; + this.strength = heroAttributes.strength; +} +Hero.prototype = Object.create(Humanoid.prototype); +Hero.prototype.attack = function() { + return `${this.name} the hero will win in the end.` +} + +// 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', + deception: 'You have just been deceived.', +}); + +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', + deception: 'You have just been deceived.', +}); + +// Stretch task: + +const Father = new Villain ({ + name: 'Sir Mustachio', + deception: 'You have just been deceived.', + poison: -1, + truth: 'The truth will guide you', + strength: 20, +}); + +const Son = new Hero ({ + name: 'Lilith', + deception: 'You have just been deceived.', + poison: -1, + truth: 'The truth will guide you', + strength: 20, +}); + +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. + +console.log(Father.stabBack()); +console.log(Son.attack()); \ No newline at end of file From b0829a17d6666dbde40eca15fd605196c95c4921 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 11 Oct 2019 23:43:11 -0400 Subject: [PATCH 2/3] JS-IV prototypes and classes --- assignments/lambda-classes.js | 83 ++++++++++++++ assignments/prototype-refactor.js | 173 +++++++++++++++++++++--------- assignments/tempCodeRunnerFile.js | 1 + 3 files changed, 209 insertions(+), 48 deletions(-) create mode 100644 assignments/tempCodeRunnerFile.js diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 71acfca0e..483c44127 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -1 +1,84 @@ // CODE here for your Lambda Classes + +// Base CLASS -- parent +class Person { + constructor(attrs) { + this.name = attrs.name; + this.age = attrs.age; + this.location = attrs.location; + } + // Methods: + speak() { + console.log(`Hello my name is ${this.name}, I am from ${this.location}`); + } +} + +// Instructor CLASS -- chiled + +class Instructor extends Person { + constructor(attrs) { + super(attrs); + this.specialty = attrs.specialty; + this.favLanguage = attrs.favLanguage; + this.catchPhrase = attrs.catchPhrase; + } + // Methods: + demo(subject) { + console.log(`Today we are learning about ${subject}`); + } + grade(student, subject) { + console.log(`${student.name} receives a perfect score on ${subject}`); + } +} + +// Student CLASS -- chiled + +class Student extends Person { + constructor(attrs) { + super(attrs); + this.previousBackground = attrs.previousBackground; + this.className = attrs.className; + this.favSubjects = attrs.favSubjects; // creates an array + } + // Methods: + listsSubjects(favSubjects) { + console.log(`My favorite subjects are: ${favSubjects}`); // prints out an array + } + PRAssignment(subject) { + console.log(`${this.name} has submitted a PR for ${subject}`); + } + sprintChallenge(subject) { + console.log(`${this.name} has begun sprint challenge on ${subject}`); + } +} + +// Project Manager CLASS -- chiled + +class ProjectManager extends Instructor { + constructor(attrs) { + super(attrs); + this.gradClassName = attrs.gradClassName; + this.favInstructor = attrs.favInstructor; + } + // Methods: + standUp(channel) { + console.log(`${this.name} announces to ${channel}, @channel standy times!​​​​​`); + } + debugsCode(studentObj, subject) { + console.log(`${this.name} debugs ${student.name}'s code on ${subject}`); + } +} + +// Calling action: + +const fred = new Instructor ({ + name: 'Fred', + location: 'Bedrock', + age: 37, + favLanguage: 'JavaScript', + specialty: 'Front-end', + catchPhrase: `Don't forget the homies`, +}); + +console.log(fred); +console.log(fred.demo(`JavaScript - IV`)); diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index 6607a5838..9334aea16 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -11,65 +11,129 @@ Prototype Refactor // === GameObject === -function GameObject(attributes){ - this.createdAt = attributes.createdAt; - this.name = attributes.name; - this.dimensions = attributes.dimensions; -} -GameObject.prototype.destroy = function() { - return `${this.name} was removed from the game.`; +// function GameObject(attributes){ +// this.createdAt = attributes.createdAt; +// this.name = attributes.name; +// this.dimensions = attributes.dimensions; +// } +// GameObject.prototype.destroy = function() { +// return `${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 === -function CharacterStats(charAttributes) { - GameObject.call(this, charAttributes); - this.healthPoints = charAttributes.healthPoints; -} - -CharacterStats.prototype = Object.create(GameObject.prototype); - -CharacterStats.prototype.takeDamage = function() { - return `${this.name} took damage.` +// function CharacterStats(charAttributes) { +// GameObject.call(this, charAttributes); +// this.healthPoints = charAttributes.healthPoints; +// } + +// CharacterStats.prototype = Object.create(GameObject.prototype); + +// CharacterStats.prototype.takeDamage = function() { +// return `${this.name} took damage.` +// } + +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.) === -function Humanoid(humanAttributes) { - CharacterStats.call(this, humanAttributes); - this.team = humanAttributes.team; - this.weapons = humanAttributes.weapons; - this.language = humanAttributes.language; -} - -Humanoid.prototype = Object.create(CharacterStats.prototype); - -Humanoid.prototype.greet = function() { - return `${this.name} offers a greeting in ${this.language}.` +// function Humanoid(humanAttributes) { +// CharacterStats.call(this, humanAttributes); +// this.team = humanAttributes.team; +// this.weapons = humanAttributes.weapons; +// this.language = humanAttributes.language; +// } + +// Humanoid.prototype = Object.create(CharacterStats.prototype); + +// Humanoid.prototype.greet = function() { +// return `${this.name} offers a greeting in ${this.language}.` +// } + +class Humanoid extends CharacterStats { + constructor(humanAttributes) { + super(humanAttributes); + this.team = humanAttributes.team; + this.weapons = humanAttributes.weapons; + this.language = humanAttributes.language; + this.V_newhealthPoints = humanAttributes.healthPoints - 5; + this.H_newhealthPoints = this.V_newhealthPoints + 1; + } + greet() { + return `${this.name} offers a greeting in ${this.language}.` + } } // === Stretch task: === -function Villain(villainAttributes) { - Humanoid.call(this, villainAttributes); - this.deception = villainAttributes.deception; - this.poison = villainAttributes.poison; +// function Villain(villainAttributes) { +// Humanoid.call(this, villainAttributes); +// this.deception = villainAttributes.deception; +// this.poison = villainAttributes.poison; +// } + +// Villain.prototype = Object.create(Humanoid.prototype); + +// Villain.prototype.stabBack = function() { +// return `Plot twist! ${this.name} has just deceived you!` +// } + +// function Hero (heroAttributes) { +// Humanoid.call(this, heroAttributes); +// this.truth = heroAttributes.truth; +// this.strength = heroAttributes.strength; +// } + +// Hero.prototype = Object.create(Humanoid.prototype); + +// Hero.prototype.attack = function() { +// return `${this.name} the hero will win in the end.` +// } + +class Villain extends Humanoid { + constructor(villainAttributes) { + super(villainAttributes); + this.deception = villainAttributes.deception; + this.poison = villainAttributes.poison; + // this.V_newhealthPoints = Son.healthPoints - 5; + } + stabBack() { + // let newhealthPoints = Son.healthPoints - 5; + console.log(`Plot twist! ${this.name} The Father has just deceived The Hero (aka The Son)!`) + console.log(`${Son.name} initial health points: ${Son.healthPoints} -- new health points total: ${Son.V_newhealthPoints}.`); + } } -Villain.prototype = Object.create(Humanoid.prototype); - -Villain.prototype.stabBack = function() { - return `Plot twist! ${this.name} has just deceived you!` -} - -function Hero (heroAttributes) { - Humanoid.call(this, heroAttributes); - this.truth = heroAttributes.truth; - this.strength = heroAttributes.strength; -} -Hero.prototype = Object.create(Humanoid.prototype); -Hero.prototype.attack = function() { - return `${this.name} the hero will win in the end.` +class Hero extends Humanoid { + constructor(heroAttributes) { + super(heroAttributes); + this.truth = heroAttributes.truth; + this.strength = heroAttributes.strength; + } + attack() { + // let newhealthPoints = this.healthPoints + 1; + console.log(`${this.name} is the hero they win in the end.`); + console.log(`${this.name} initial health points: ${Son.V_newhealthPoints} -- new health points total: ${Son.H_newhealthPoints}.`); + } } // Test you work by un-commenting these 3 objects and the list of console logs below: @@ -125,7 +189,6 @@ const archer = new Humanoid({ 'Dagger', ], language: 'Elvish', - deception: 'You have just been deceived.', }); // Stretch task: @@ -139,7 +202,20 @@ const Father = new Villain ({ }); const Son = new Hero ({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4, + }, + healthPoints: 10, name: 'Lilith', + team: 'Forest Kingdom', + weapons: [ + 'Bow', + 'Dagger', + ], + language: 'Elvish', deception: 'You have just been deceived.', poison: -1, truth: 'The truth will guide you', @@ -157,5 +233,6 @@ 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. -console.log(Father.stabBack()); -console.log(Son.attack()); \ No newline at end of file +console.log(Father.stabBack()); // Health Points - 5 +console.log(Son.attack()); // Health Points + 1 +// console.log(Father.stabBack()); // Health Points - 5 \ No newline at end of file diff --git a/assignments/tempCodeRunnerFile.js b/assignments/tempCodeRunnerFile.js new file mode 100644 index 000000000..4b2cb1501 --- /dev/null +++ b/assignments/tempCodeRunnerFile.js @@ -0,0 +1 @@ +console.log(`${this.name} initial health points: ${V_newhealthPoints} -- new health points total: ${H_newhealthPoints}.`); \ No newline at end of file From c935c2cd8fc3f16ffcc3914bf2807a4ca3296384 Mon Sep 17 00:00:00 2001 From: Jessica Date: Sat, 12 Oct 2019 13:05:17 -0400 Subject: [PATCH 3/3] stretch --- assignments/lambda-classes.js | 54 +++++++++++++++++++++++++++---- assignments/prototype-refactor.js | 1 + assignments/tempCodeRunnerFile.js | 2 +- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 483c44127..a76f1bea0 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -29,6 +29,15 @@ class Instructor extends Person { grade(student, subject) { console.log(`${student.name} receives a perfect score on ${subject}`); } + subtractPoints(student, subject, grade) { + console.log(`${student} grade in ${subject}: `); + console.log(`Student grade: ${grade} - New Student grade: ${grade - Math.random()}`); + } + addPoints(student, subject, grade) { + console.log(`${student} grade in ${subject}: `); + let newGrade = grade + Math.random(); + console.log(`Student grade: ${grade} - New Student grade: ${newGrade}`); + } } // Student CLASS -- chiled @@ -38,11 +47,15 @@ class Student extends Person { super(attrs); this.previousBackground = attrs.previousBackground; this.className = attrs.className; - this.favSubjects = attrs.favSubjects; // creates an array + this.grade = attrs.grade; + this.fafavSubjects = attrs.favSubjects; } // Methods: - listsSubjects(favSubjects) { - console.log(`My favorite subjects are: ${favSubjects}`); // prints out an array + listsSubjects(fafavSubjects) { + console.log(`Hello my name is ${jane.name}. My favorite subjects are: `); + for (let i = 0; i < fafavSubjects.length; i++) { + console.log(`subject # ${i + 1}: ${fafavSubjects[i]}`); // prints out an array + } } PRAssignment(subject) { console.log(`${this.name} has submitted a PR for ${subject}`); @@ -69,16 +82,45 @@ class ProjectManager extends Instructor { } } -// Calling action: + +// Calling action: const fred = new Instructor ({ name: 'Fred', location: 'Bedrock', age: 37, favLanguage: 'JavaScript', - specialty: 'Front-end', + specialty: 'Back-end', catchPhrase: `Don't forget the homies`, }); -console.log(fred); +const jane = new Student ({ + name: 'Jane', + location: 'Verginia', + age: 25, + previousBackground: 'Teacher', + className: 'WEBPT11', + grade: 87, + favSubjects: ['Html', 'CSS', 'JavaScript'], +}); + +const eric = new ProjectManager ({ + name: 'Eric', + location: 'Colorado', + age: 30, + favLanguage: 'JavaScript', + specialty: 'Front-end', + catchPhrase: `say what`, + gradClassName: 'WEBPT11', + favInstructor: 'Fred', +}); + + +console.log(jane.listsSubjects(jane.fafavSubjects)); +console.log(jane.grade); +console.log(fred.specialty); console.log(fred.demo(`JavaScript - IV`)); +console.log(fred.subtractPoints(jane.name, `JavaScript - IV`, jane.grade)); +console.log(fred.addPoints(jane.name, `JavaScript - IV`, jane.grade)); + + diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index 9334aea16..ad117b048 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -77,6 +77,7 @@ class Humanoid extends CharacterStats { this.language = humanAttributes.language; this.V_newhealthPoints = humanAttributes.healthPoints - 5; this.H_newhealthPoints = this.V_newhealthPoints + 1; + this.Total_healthPoints = this.H_newhealthPoints; } greet() { return `${this.name} offers a greeting in ${this.language}.` diff --git a/assignments/tempCodeRunnerFile.js b/assignments/tempCodeRunnerFile.js index 4b2cb1501..453c01b49 100644 --- a/assignments/tempCodeRunnerFile.js +++ b/assignments/tempCodeRunnerFile.js @@ -1 +1 @@ -console.log(`${this.name} initial health points: ${V_newhealthPoints} -- new health points total: ${H_newhealthPoints}.`); \ No newline at end of file +console.log(Father.stabBack()); // Health Points - 5 \ No newline at end of file