From 9f4c9a8ba92eab990a2919026899c953ad28ea50 Mon Sep 17 00:00:00 2001 From: Ahmed Belhadj Date: Thu, 24 May 2018 14:31:08 -0500 Subject: [PATCH 1/3] Ahmed Belhadj - JavaScript IV Converted all constructors into ES6 Classes using the class and extends keywords. --- assignments/lambda-classes.js | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 71acfca0e..6ce852fb6 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -1 +1,87 @@ // CODE here for your Lambda Classes + +class GameObject{ + constructor(options){ + this.createdAt = options.createdAt; + this.dimensions = options.dimensions; + } + destroy(){ + return `${this.name} was removed from the game.`; + } +} + +class CharacterStats extends GameObject{ + constructor(characterStatsOptions){ + super(characterStatsOptions); + this.hp = characterStatsOptions.hp; + this.name = characterStatsOptions.name; + } + takeDamage(){ + return `${this.name} took damage.`; + } +} + +class Humanoid extends CharacterStats{ + constructor(humanoidOptions){ + super(humanoidOptions); + this.faction = humanoidOptions.faction; + this.weapons = humanoidOptions.weapons; + this.language = humanoidOptions.language; + } + greet(){ + return `${this.name} offers a greeting in ${this.language}.`; + } +} + +const mage = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1 + }, + hp: 5, + name: 'Bruce', + faction: 'Mage Guild', + weapons: ['Staff of Shamalama'], + language: 'Common Toungue' +}); + +const swordsman = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 2, + height: 2 + }, + hp: 15, + name: 'Sir Mustachio', + faction: 'The Round Table', + weapons: ['Giant Sword', 'Shield'], + language: 'Common Toungue' +}); + +const archer = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4 + }, + hp: 10, + name: 'Lilith', + faction: '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.hp); // 15 +console.log(mage.name); // Bruce +console.log(swordsman.faction); // 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. \ No newline at end of file From 1522c39b63a0989f256860a806308b63e993017d Mon Sep 17 00:00:00 2001 From: Ahmed Belhadj Date: Thu, 24 May 2018 15:29:29 -0500 Subject: [PATCH 2/3] Ahmed Belhadj - JavaScript IV Completed lambda-classes. --- assignments/lambda-classes.js | 154 ++++++++++++++++-------------- assignments/prototype-refactor.js | 57 ++++++----- 2 files changed, 112 insertions(+), 99 deletions(-) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 6ce852fb6..0beb7455b 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -1,87 +1,101 @@ // CODE here for your Lambda Classes -class GameObject{ - constructor(options){ - this.createdAt = options.createdAt; - this.dimensions = options.dimensions; +class Person{ + constructor(attributes){ + this.name = attributes.name; + this.age = attributes.age; + this.location = attributes.location; + this.gender = attributes.gender; } - destroy(){ - return `${this.name} was removed from the game.`; + speak(){ + return `Hello my name is ${this.name}, I am from ${this.location}.`; } } -class CharacterStats extends GameObject{ - constructor(characterStatsOptions){ - super(characterStatsOptions); - this.hp = characterStatsOptions.hp; - this.name = characterStatsOptions.name; +class Instructor extends Person{ + constructor(attributes){ + super(attributes); + this.specialty = attributes.specialty; + this.favLanguage = attributes.favLanguage; + this.catchPhrase = attributes.catchPhrase; } - takeDamage(){ - return `${this.name} took damage.`; + demo(subject){ + return `Today we are learning about ${subject}.`; + } + grade(student, subject){ + return `${student.name} receives a perfect score on ${subject}.` } } -class Humanoid extends CharacterStats{ - constructor(humanoidOptions){ - super(humanoidOptions); - this.faction = humanoidOptions.faction; - this.weapons = humanoidOptions.weapons; - this.language = humanoidOptions.language; +class Student extends Person{ + constructor(attributes){ + super(attributes); + this.previousBackground = attributes.previousBackground; + this.className = attributes.className; + this.favSubjects = attributes.favSubjects; + } + listSubjects(){ + return this.favSubjects; + } + PRAssignment(subject){ + return `${this.name} has submitted a PR for ${subject}.`; } - greet(){ - return `${this.name} offers a greeting in ${this.language}.`; + sprintChallenge(subject){ + return `${this.name} has begun spring challenge on ${subject}.`; } } -const mage = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 1, - height: 1 - }, - hp: 5, - name: 'Bruce', - faction: 'Mage Guild', - weapons: ['Staff of Shamalama'], - language: 'Common Toungue' -}); - -const swordsman = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 2, - height: 2 - }, - hp: 15, - name: 'Sir Mustachio', - faction: 'The Round Table', - weapons: ['Giant Sword', 'Shield'], - language: 'Common Toungue' +class ProjectManager extends Instructor{ + constructor(attributes){ + super(attributes); + this.gradClass = attributes.gradClass; + this.favInstructor = attributes.favInstructor; + } + standUp(channel){ + return `${this.name} announces to ${channel}, @channel standy times!`; + } + debugsCode(student, subject){ + return `${this.name} debugs ${student.name}'s code on ${subject}.`; + } +} + +const fred = new Instructor({ + name: 'Fred', + location: 'Bedrock', + age: 37, + gender: 'male', + favLanguage: 'JavaScript', + specialty: 'Front-end', + catchPhrase: `Don't forget the homies` + }); + +const wilma = new Student({ + name: 'Wilma', + location: 'Couchrock', + previousBackground: 'waitress', + className: 'CS132', + favSubjects: ['HTML', 'CSS', 'JavaScript'] }); - -const archer = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 1, - width: 2, - height: 4 - }, - hp: 10, - name: 'Lilith', - faction: 'Forest Kingdom', - weapons: ['Bow', 'Dagger'], - language: 'Elvish' + +const pebbles = new ProjectManager({ + name: 'Pebbles', + location: 'Bedstone', + gradClass: 'CS1', + favInstructor: 'Sean' }); -console.log(mage.createdAt); // Today's date -console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } -console.log(swordsman.hp); // 15 -console.log(mage.name); // Bruce -console.log(swordsman.faction); // 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. \ No newline at end of file +console.log(fred); +console.log(wilma); +console.log(pebbles); +console.log(fred.speak()); +console.log(wilma.speak()); +console.log(pebbles.speak()); +console.log(fred.demo("Redux")); +console.log(fred.grade(wilma, "JavaScript")); +console.log(wilma.listSubjects()); +console.log(wilma.PRAssignment("Python")); +console.log(wilma.sprintChallenge("Elm")); +console.log(pebbles.demo("HTML")); +console.log(pebbles.grade(wilma, "CSS")); +console.log(pebbles.standUp("CS132")); +console.log(pebbles.debugsCode(wilma, "JavaScript")); diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index e55ae39c0..026184411 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -2,40 +2,39 @@ // Today 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. -function GameObject(options) { - this.createdAt = options.createdAt; - this.dimensions = options.dimensions; +class GameObject{ + constructor(options){ + this.createdAt = options.createdAt; + this.dimensions = options.dimensions; + } + destroy(){ + return `${this.name} was removed from the game.`; + } } -GameObject.prototype.destroy = function() { - return `Object was removed from the game.`; -}; - -function CharacterStats(characterStatsOptions) { - GameObject.call(this, characterStatsOptions); - this.hp = characterStatsOptions.hp; - this.name = characterStatsOptions.name; +class CharacterStats extends GameObject{ + constructor(characterStatsOptions){ + super(characterStatsOptions); + this.hp = characterStatsOptions.hp; + this.name = characterStatsOptions.name; + } + takeDamage(){ + return `${this.name} took damage.`; + } } -CharacterStats.prototype = Object.create(GameObject.prototype); - -CharacterStats.prototype.takeDamage = function() { - return `${this.name} took damage.`; -}; - -function Humanoid(humanoidOptions) { - CharacterStats.call(this, humanoidOptions); - this.faction = humanoidOptions.faction; - this.weapons = humanoidOptions.weapons; - this.language = humanoidOptions.language; +class Humanoid extends CharacterStats{ + constructor(humanoidOptions){ + super(humanoidOptions); + this.faction = humanoidOptions.faction; + this.weapons = humanoidOptions.weapons; + this.language = humanoidOptions.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}.`; -}; - const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -87,4 +86,4 @@ 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(swordsman.destroy()); // Sir Mustachio was removed from the game. \ No newline at end of file From 40b100fd5db1041a376f3cccdee714a211e60bd7 Mon Sep 17 00:00:00 2001 From: Ahmed Belhadj Date: Thu, 24 May 2018 16:17:47 -0500 Subject: [PATCH 3/3] Ahmed Belhadj - JavaScript IV Completed stretch problem. --- assignments/lambda-classes.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 0beb7455b..8b701c3b7 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -25,6 +25,10 @@ class Instructor extends Person{ grade(student, subject){ return `${student.name} receives a perfect score on ${subject}.` } + gradeAssignment(student){ + student.grade += (Math.random() * (50 - (-50)) + (-50)); + return `${student.name}'s grade is ${student.grade}.`; + } } class Student extends Person{ @@ -33,6 +37,7 @@ class Student extends Person{ this.previousBackground = attributes.previousBackground; this.className = attributes.className; this.favSubjects = attributes.favSubjects; + this.grade = attributes.grade; } listSubjects(){ return this.favSubjects; @@ -43,6 +48,14 @@ class Student extends Person{ sprintChallenge(subject){ return `${this.name} has begun spring challenge on ${subject}.`; } + graduate(instructor){ + if(this.grade >= 70){ + return `${this.name} has graduated.`; + } + else{ + return instructor.gradeAssignment(this); + } + } } class ProjectManager extends Instructor{ @@ -74,7 +87,8 @@ const wilma = new Student({ location: 'Couchrock', previousBackground: 'waitress', className: 'CS132', - favSubjects: ['HTML', 'CSS', 'JavaScript'] + favSubjects: ['HTML', 'CSS', 'JavaScript'], + grade: 50 }); const pebbles = new ProjectManager({ @@ -99,3 +113,5 @@ console.log(pebbles.demo("HTML")); console.log(pebbles.grade(wilma, "CSS")); console.log(pebbles.standUp("CS132")); console.log(pebbles.debugsCode(wilma, "JavaScript")); +console.log(fred.gradeAssignment(wilma)); +console.log(wilma.graduate(fred)); \ No newline at end of file