From 923032f56d544a0bdc8075548615ddbfd7de1e1a Mon Sep 17 00:00:00 2001 From: Hunter5555 Date: Thu, 19 Sep 2019 20:13:45 -0600 Subject: [PATCH] almost done --- assignments/lambda-classes.js | 157 +++++++++++++++++++++ assignments/prototype-refactor.js | 220 +++++++++++++++++++++++++++++- 2 files changed, 376 insertions(+), 1 deletion(-) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 71acfca0e..0e059f63e 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -1 +1,158 @@ // CODE here for your Lambda Classes +class Person { + constructor(personAttrs) { + this.name = personAttrs.name; + this.age = personAttrs.age; + this.location = personAttrs.location; + this.gender = personAttrs.gender; + } + + speak() { + return `Hello my name is ${this.name}, I am from ${this.location}`; + } +} + +class Instructor extends Person { + constructor(instructorAttrs) { + super(instructorAttrs); + this.specialty = instructorAttrs.specialty; + this.favLanguage = instructorAttrs.favLanguage; + this.catchPhrase = instructorAttrs.catchPhrase; + } + + demo(subject) { + return `Today we are learning about ${subject}`; + } + + grade(student, subject) { + return `${student.name} receives a perfect score on ${subject}` + } + + adjustGrade(student) { + let points = Math.round(Math.random() * 100); + if (student.grade >= 100) { + student.grade -= points; + return `${points} points are subtracted from ${student.name}'s grade. ${student.name}'s current grade is ${student.grade}`; + } else { + student.grade += points; + return `${points} points are added to ${student.name}'s grade. ${student.name}'s current grade is ${student.grade}`; + } + } +} + +class Student extends Person { + constructor(studentAttrs) { + super(studentAttrs); + this.previousBackground = studentAttrs.previousBackground; + this.className = studentAttrs.className; + this.favSubjects = studentAttrs.favSubjects; + this.grade = studentAttrs.grade; + } + + sprintChallenge(subject) { + return `${this.name} has begun sprint challenge on ${subject}`; + } +listsSubjects() { + this.favSubjects.map(item => console.log(item)); + } + + PRAssignment(subject) { + return `${this.name} has submitted a PR for ${subject}`; + } + + + graduate() { + if (this.grade >= 70) { + return `${this.name} has graduated with a final grade of ${this.grade}!`; + } else { + let diff = 70 - this.grade; + this.grade += diff; + return `After more grading, ${diff} points were earned and ${this.name} has graduated with a final grade of ${this.grade}!`; + } + } +} + +class ProjectManager extends Instructor { + constructor(managerAttrs) { + super(managerAttrs); + this.gradClassName = managerAttrs.gradClassName; + this.favInstructor = managerAttrs.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 brit = new Instructor({ + name: "Brit", + location: "Canada", + age: 31, + gender: "female", + favLanguage: "JavaScript", + specialty: "Front-end", + catchPhrase: `We'll go ahead and do some pair programming. ` +}); + +const don = new Instructor({ + name: "Don", + location: "Indiana", + age: 38, + gender: "male", + favLanguage: "CSS", + specialty: "Fullstack", + catchPhrase: `Ready` +}); + +const john = new Student({ + name: "Hunter", + location: "Salt Lake City", + age: 21, + gender: "male", + favSubjects: ["Javascript"], + grade: 100 +}); + +const andi = new Student({ + name: "andy", + location: "Tokyo", + age: 26, + gender: "female", + favSubjects: ["Javascript"], + grade: 100 +}); + +const shawn = new ProjectManager({ + name: "Shawn", + location: "Toledo", + age: 58, + gender: "male", + gradClassName: "CS1" +}); + +const tina = new ProjectManager({ + name: "Tina", + location: "Scranton", + age: 30, + gender: "female", + gradClassName: "CS2" +}); + + +console.log(tina.speak()); +console.log(jen.listsSubjects()); +console.log(shawn.gender); +console.log(tina.debugsCode(jen, "Javascript")); +console.log(shawn.standUp("FSW14")); +console.log(jen.sprintChallenge("Javascript")); +console.log(brit.demo("React")); +console.log(brit.grade(jen, "CSS")); +console.log(shawn.speak()); +console.log(john.PRAssignment("Responsive Design")); +console.log(don.catchPhrase); +console.log(tina.adjustGrade(jen)); +console.log(jen.graduate(brit)); \ No newline at end of file diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index 91424c9fa..58e67f4de 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -6,4 +6,222 @@ 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. -*/ +*/ class GameObject{ + constructor(gameObejectAttributes){ + this.createdAt = gameObejectAttributes.createdAt; + this.dimensions = gameObejectAttributes.dimensions; + } + //methods for this ^ constructor + destroy() { + return `${this.name} was removed from the game`; + } +} + //Build next constructor + class CharacterStats{ + constructor(characterStatsAttribute){ + this.hp = characterStatsAttribute.hp; + this.name = characterStatsAttribute.name; + GameObject.call(this, characterStatsAttribute); + } + + // Inheritance + class CharacterStats extends GameObject{ + constructor(characterStatsAttribute) + super(characterStatsAttribute); + } + + //methods for this ^ constructor + takeDamage() { + return `${this.name} took damage`; + } +} + //Test you work by uncommenting these 3 objects and the list of console logs below: + class Humanoid{ + constructor(humanoidAttributes){ + this.faction = humanoidAttributes.faction; + this.weapons = humanoidAttributes.weapons; + this.language = humanoidAttributes.language; + CharacterStats.call(this,humanoidAttributes); + } + + Humanoid.prototype = Object.create(CharacterStats.prototype); + //Humanoid methods under here. + //greet() // prototype method -> returns the string ' offers a greeting in .' + //^^ building above. + greet() { + return `${this.name} says 'hI' 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 Tongue', + }); + + + 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: 'Misiones, Argentina', + weapons: [ + 'Bow', + 'Dagger', + ], + language: 'Elvish', + }); + // new constructor + class Villian{ + constructor(villianAttributes){ + Humanoid.call(this, villianAttributes); + this.badBreath = villianAttributes.badBreath; + this.opponentHealth = villianAttributes.opponentHealth; + } + //inherit + Villian.prototype = Object.create(Humanoid.prototype); + + //methods + evilLaugh() { + this.opponentHealth -= 10; + return `The villian ${this.name} lets out an evil laugh you will fail because I am ${this.name}`; + } +} + // const failure = new Villian({ + // createdAt: new Date(), + // dimensions: { + // length: 1, + // width: 2, + // height: 4, + // }, + // hp: 10, + // name: 'Failure', + // faction: 'Misiones, Argentina', + // weapons: [ + // 'doubt','low confidence' + // ], + // language: 'Marlarkey', + // badBreath: 'Janky' + // }); + + class Hero{ + + constructor(heroAttributes){ + this.goodLooks = heroAttributes.goodlooks; + this.opponentHealth = heroAttributes.opponentHealth; + + Humanoid.call(this, heroAttributes); + } + + Hero.prototype = Object.create(Humanoid.prototype); + + Hero.prototype.studyHard = function () { + this.opponentHealth -=20; + console.log(this.opponent); + return `The hero ${this.name} studies hard`; + } + Hero.prototype.workHard = function () { + this.opponentHealth -=50; + return `The hero ${this.name} works hard` + } + Hero.prototype.flawlessVictory = function() { + this.opponentHealth-= 30; + return `The hero learns to fight using his weapons ${this.weapons}... and defeats the villian`; + } + + + const Hunter = new Hero({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4, + }, + hp: 10, + name: 'Hunter', + faction: 'Misiones, Argentina', + weapons: [ + 'determination', 'work ethic', 'great instructors', 'supporters', 'the list goes on and on...seriously' + ], + language: 'JavaScript', + goodLooks: 'High', + opponentHealth: 100 + }); + + failure = new Villian({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4, + }, + hp: 10, + name: 'Failure', + faction: 'Misiones, Argentina', + weapons: [ + 'doubt','low confidence' + ], + language: 'Guarani', + badBreath: 'Janky', + opponentHealth: 100 + }); + + 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. + + + console.log("A new battle is about to begin") + console.log(`The match ${Hunter.name} vs ${failure.name} has begun.`); + console.log(`The villian has the following weapons ${failure.weapons}`); + console.log(`The hero has the following weapons ${Hunter.weapons}`); + console.log(`The villian has the following extra attribute ${failure.badBreath} breath`); + console.log(`The villian speaks the following language ${failure.language}`); + console.log(`The hero speaks the following language ${Hunter.language}`); + console.log(failure.evilLaugh()); + console.log(`${Hunter.name} has been reduced, ${Hunter.name} now has ${failure.opponentHealth}`); + console.log(Hunter.studyHard()); + console.log(`${failure.name} has been reduced, ${failure.name} now has ${failure.opponentHealth}`); + console.log(Hunter.workHard()); + console.log(`${failure.name} has been reduced, ${failure.name} now has ${Hunter.opponentHealth}`) ; + console.log(Hunter.flawlessVictory()); + console.log(`${failure.name} has been reduced, ${Hunter.opponentHealth} now has ${Hunter.opponentHealth}`); + console.log(`${Hunter.name} saves the Guarani from conquest!`);