diff --git a/README.md b/README.md index f944d234d..cbab650f3 100644 --- a/README.md +++ b/README.md @@ -6,115 +6,118 @@ This challenge focuses on classes in JavaScript using the new `class` keyword. **Follow these steps to set up and work on your project:** -* [ ] Create a forked copy of this project. -* [ ] Add your project manager as collaborator on Github. -* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!). -* [ ] Create a new branch: git checkout -b ``. -* [ ] Implement the project on your newly created `` branch, committing changes regularly. -* [ ] Push commits: git push origin ``. +- [ ] Create a forked copy of this project. +- [ ] Add your project manager as collaborator on Github. +- [ ] Clone your OWN version of the repository (Not Lambda's by mistake!). +- [ ] Create a new branch: git checkout -b ``. +- [ ] Implement the project on your newly created `` branch, committing changes regularly. +- [ ] Push commits: git push origin ``. **Follow these steps for completing your project.** -* [ ] Submit a Pull-Request to merge Branch into master (student's Repo). **Please don't merge your own pull request** -* [ ] Add your project manager as a reviewer on the pull-request -* [ ] Your project manager will count the project as complete by merging the branch back into master. +- [ ] Submit a Pull-Request to merge Branch into master (student's Repo). **Please don't merge your own pull request** +- [ ] Add your project manager as a reviewer on the pull-request +- [ ] Your project manager will count the project as complete by merging the branch back into master. ## Assignment Description You already pretty much know all about classes but you're used to seeing them built in the following context: ```js -function Person(personAttributes) { +function Person(personAttributes) { this.name = personAttributes.name; this.age = personAttributes.age; this.location = personAttributes.location; } const fred = new Person({ - name: 'Fred', + name: "Fred", age: 37, - location: 'Bedrock' + location: "Bedrock" }); ``` -* Because none of the above code is new, you're about to see your world get much much easier when dealing with Object Creation and Classical Inheritance as it pertains to JavaScript. -* The Class Keyword makes this SO MUCH EASIER! -* **Fork** and clone this repository. -* **Complete** all of the exercises found in the assignment files. +- Because none of the above code is new, you're about to see your world get much much easier when dealing with Object Creation and Classical Inheritance as it pertains to JavaScript. +- The Class Keyword makes this SO MUCH EASIER! +- **Fork** and clone this repository. +- **Complete** all of the exercises found in the assignment files. ## `prototype-refactor` - Take existing code and make it modern. -* You're going to work with your prototypes assignment you built out yesterday. -* `Challenge:` **Convert** all of your constructors into ES6 Classes using the `class` and `extends` keywords. You should be able to run your same logs and they should build out the proper expected behaviors. +- You're going to work with your prototypes assignment you built out yesterday. +- `Challenge:` **Convert** all of your constructors into ES6 Classes using the `class` and `extends` keywords. You should be able to run your same logs and they should build out the proper expected behaviors. ## `lambda-classes` - We need a roster of Lambda School personnel. Build it! -* We have a school to build here! This project will get you used to thinking about classes in JavaScript and building them from a brand new data set. -* Lambda personnel can be broken down into three different types of `people`. - * **Instructors** - extensions of Person - * **Students** - extensions of Person - * **Project Managers** - extensions of Instructors -* **IMPORTANT** - You'll need to create 2 - 3 objects for each class and test them according to their unique Attributes. For example: +- We have a school to build here! This project will get you used to thinking about classes in JavaScript and building them from a brand new data set. +- Lambda personnel can be broken down into three different types of `people`. + - **Instructors** - extensions of Person + - **Students** - extensions of Person + - **Project Managers** - extensions of Instructors +- **IMPORTANT** - You'll need to create 2 - 3 objects for each class and test them according to their unique Attributes. For example: ```js const fred = new Instructor({ - name: 'Fred', - location: 'Bedrock', + name: "Fred", + location: "Bedrock", age: 37, - gender: 'male', - favLanguage: 'JavaScript', - specialty: 'Front-end', + gender: "male", + favLanguage: "JavaScript", + specialty: "Front-end", catchPhrase: `Don't forget the homies` }); ``` #### Person -* First we need a Person class. This will be our `base-class` -* Person receives `name` `age` `location` `gender` all as props -* Person receives `speak` as a method. -* This method logs out a phrase `Hello my name is Fred, I am from Bedrock` where `name` and `location` are the object's own props +- First we need a Person class. This will be our `base-class` +- Person receives `name` `age` `location` `gender` all as props +- Person receives `speak` as a method. +- This method logs out a phrase `Hello my name is Fred, I am from Bedrock` where `name` and `location` are the object's own props #### Instructor -* Now that we have a Person as our base class, we'll build our Instructor class. -* Instructor uses the same attributes that have been set up by Person -* Instructor has the following unique props: - * `specialty` what the Instructor is good at i.e. 'redux' - * `favLanguage` i.e. 'JavaScript, Python, Elm etc.' - * `catchPhrase` i.e. `Don't forget the homies` -* Instructor has the following methods: - * `demo` receives a `subject` string as an argument and logs out the phrase 'Today we are learning about {subject}' where subject is the param passed in. - * `grade` receives a `student` object and a `subject` string as arguments and logs out '{student.name} receives a perfect score on {subject}' +- Now that we have a Person as our base class, we'll build our Instructor class. +- Instructor uses the same attributes that have been set up by Person +- Instructor has the following unique props: + - `specialty` what the Instructor is good at i.e. 'redux' + - `favLanguage` i.e. 'JavaScript, Python, Elm etc.' + - `catchPhrase` i.e. `Don't forget the homies` +- Instructor has the following methods: + - `demo` receives a `subject` string as an argument and logs out the phrase 'Today we are learning about {subject}' where subject is the param passed in. + - `grade` receives a `student` object and a `subject` string as arguments and logs out '{student.name} receives a perfect score on {subject}' #### Student -* Now we need some students! -* Student uses the same attributes that have been set up by Person -* Student has the following unique props: - * `previousBackground` i.e. what the Student used to do before Lambda School - * `className` i.e. CS132 - * `favSubjects`. i.e. an array of the student's favorite subjects ['Html', 'CSS', 'JavaScript'] -* Student has the following methods: - * `listsSubjects` a method that logs out all of the student's favoriteSubjects one by one. - * `PRAssignment` a method that receives a subject as an argument and logs out that the `student.name has submitted a PR for {subject}` - * `sprintChallenge` similar to PRAssignment but logs out `student.name has begun sprint challenge on {subject}` +- Now we need some students! +- Student uses the same attributes that have been set up by Person +- Student has the following unique props: + - `previousBackground` i.e. what the Student used to do before Lambda School + - `className` i.e. CS132 + - `favSubjects`. i.e. an array of the student's favorite subjects ['Html', 'CSS', 'JavaScript'] +- Student has the following methods: + - `listsSubjects` a method that logs out all of the student's favoriteSubjects one by one. + - `PRAssignment` a method that receives a subject as an argument and logs out that the `student.name has submitted a PR for {subject}` + - `sprintChallenge` similar to PRAssignment but logs out `student.name has begun sprint challenge on {subject}` #### Project Manager -* Now that we have instructors and students, we'd be nowhere without our PM's -* ProjectManagers are extensions of Instructors -* ProjectManagers have the following unique props: - * `gradClassName`: i.e. CS1 - * `favInstructor`: i.e. Sean -* ProjectManagers have the following Methods: - * `standUp` a method that takes in a slack channel and logs `{name} announces to {channel}, @channel standy times!​​​​​ - * `debugsCode` a method that takes in a student object and a subject and logs out `{name} debugs {student.name}'s code on {subject}` +- Now that we have instructors and students, we'd be nowhere without our PM's +- ProjectManagers are extensions of Instructors +- ProjectManagers have the following unique props: + - `gradClassName`: i.e. CS1 + - `favInstructor`: i.e. Sean +- ProjectManagers have the following Methods: + - `standUp` a method that takes in a slack channel and logs `{name} announces to {channel}, @channel standy times!​​​​​ + - `debugsCode` a method that takes in a student object and a subject and logs out `{name} debugs {student.name}'s code on {subject}` #### Stretch Problem -* Extend the functionality of the Student by adding a prop called grade and setting it equal to a number between 1-100. -* Now that our students have a grade build out a method on the Instructor (this will be used by _BOTH_ instructors and PM's) that will randomly add or subtract points to a student's grade. _Math.random_ will help. -* Add a graduate method to a student. - * This method, when called, will check the grade of the student and see if they're ready to graduate from Lambda School - * If the student's grade is above a 70% let them graduate! Otherwise go back to grading their assignments to increase their score. +- Extend the functionality of the Student by adding a prop called grade and setting it equal to a number between 1-100. +- Now that our students have a grade build out a method on the Instructor (this will be used by _BOTH_ instructors and PM's) that will randomly add or subtract points to a student's grade. _Math.random_ will help. +- Add a graduate method to a student. + + - This method, when called, will check the grade of the student and see if they're ready to graduate from Lambda School + - If the student's grade is above a 70% let them graduate! Otherwise go back to grading their assignments to increase their score. + + console.log(john.grade()) diff --git a/assignments/index.html b/assignments/index.html index 2fc751cde..f765acce4 100644 --- a/assignments/index.html +++ b/assignments/index.html @@ -1,17 +1,17 @@ - + - - - + + + - JS IV + JS IV - - - + + + - -

JS IV - Check your work in the console!

- - \ No newline at end of file + +

JS IV - Check your work in the console!

+ + diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 71acfca0e..d4c8df0d0 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -1 +1,189 @@ // CODE here for your Lambda Classes + +// person class + +class Person { + constructor(attributes) { + this.name = attributes.name; + this.age = attributes.age; + this.location = attributes.location; + this.gender = attributes.gender; + } + speak() { + console.log(`Hello my name is ${this.name}, I am from ${this.location}.`); + } +} + +// instructor class + +class Instructor extends Person { + constructor(instructorAttributes) { + super(instructorAttributes); + this.specialty = instructorAttributes.specialty; + this.favLanguage = instructorAttributes.favLanguage; + this.catchPhrase = instructorAttributes.catchPhrase; + } + demo(subject) { + console.log(`Today we are learning about ${subject}.`); + } + grade(x, subject) { + if (x.grade > 80) { + console.log( + `${x.name} receives a perfect score of ${x.grade} on ${subject}.` + ); + } else { + console.log( + `${x.name} receives a weak score of ${x.grade} on ${subject}.` + ); + } + } +} + +// student class + +class Student extends Instructor { + constructor(studentAttributes) { + super(studentAttributes); + this.previousBackground = studentAttributes.previousBackground; + this.className = studentAttributes.className; + this.favSubjects = studentAttributes.favSubjects; + this.grade = studentAttributes.grade; + } + listsSubjects() { + for (let i = 0; i < this.favSubjects.length; i++) { + console.log(this.favSubjects[i]); + } + } + PRAssignment() { + console.log(`${this.name} has submitted a PR for ${this.subject}.`); + } + sprintChallenge() { + console.log(`${this.name} has begun sprint challenge on ${this.subject}.`); + } + graduate() { + if (this.grade > 80) { + console.log(`${this.name} you are ready to graduate.`); + } else { + console.log(`You need your grade to be over 80.`); + } + } +} + +// projectmanager class + +class ProjectManager extends Instructor { + constructor(projectManagerAttributes) { + super(projectManagerAttributes); + this.gradClassName = projectManagerAttributes.gradClassName; + this.favInstructor = projectManagerAttributes.favInstructor; + this.channel = projectManagerAttributes.channel; + } + standUp() { + console.log( + `${this.name} announces to ${this.channel}, @${ + this.channel + } standUp times;` + ); + } + debugsCode() { + console.log(`${this.name} debugs `); + } +} + +// person objects + +const fred = new Person({ + name: "Fred", + age: 37, + location: "Bedrock", + gender: "Male" +}); + +const laura = new Person({ + name: "Laura", + age: 34, + location: "Pillowrock", + gender: "Female" +}); + +// instructor objects + +const john = new Instructor({ + name: "John", + age: 40, + location: "Mars", + gender: "Male", + specialty: "React", + language: "JavaScript", + catchPhrase: "good one!" +}); + +const marie = new Instructor({ + name: "Marie", + age: 50, + location: "Jupiter", + gender: "Female", + specialty: "JQuery", + language: "JavaScript", + catchPhrase: "this is it!" +}); + +// student objects + +const tenzing = new Student({ + name: "Tenzing", + age: 35, + location: "Moon", + gender: "Male", + specialty: "BootStrap", + language: "JavaScript", + catchPhrase: "when moon!", + previousBackground: "Sandwich Maker", + className: "WEB18", + favSubjects: ["Programming", "Investing", "Games"], + grade: Math.floor(Math.random() * 101) +}); + +const dolma = new Student({ + name: "Dolma", + age: 33, + location: "Venus", + gender: "Female", + specialty: "CSS", + language: "JavaScript", + catchPhrase: "I am good!", + previousBackground: "Designer", + className: "WEB118", + favSubjects: ["Fashion", "Investing", "Social Science"], + grade: Math.floor(Math.random() * 101) +}); + +// projectmanager objects + +const peter = new ProjectManager({ + name: "Peter", + age: 50, + location: "Galaxy", + gender: "Male", + specialty: "Django", + language: "Python", + catchPhrase: "time is up!", + gradClassName: "CS1", + favInstructor: "john", + channel: "codingDojos" +}); + +const maxie = new ProjectManager({ + name: "Maxie", + age: 40, + location: "Riverside", + gender: "Female", + specialty: "Sql", + language: "Ruby", + catchPhrase: "time is money!", + gradClassName: "CS4", + favInstructor: "larry", + channel: "codingDojos" +}); + +console.log(john.grade(dolma, "python")); diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index 91424c9fa..859ded95e 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -7,3 +7,163 @@ 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.` +*/ +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 === + * healthPoints + * takeDamage() // prototype method -> returns the string ' took damage.' + * should inherit destroy() from GameObject's prototype +*/ + +class CharacterStats extends GameObject { + constructor(characterAttributes) { + super(characterAttributes); + this.healthPoints = characterAttributes.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 +*/ + +class Humanoid extends CharacterStats { + constructor(humanoidAttributes) { + super(humanoidAttributes); + this.team = humanoidAttributes.team; + this.weapons = humanoidAttributes.weapons; + this.language = this.team = 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" +}); + +const hero = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 3, + width: 4, + height: 5 + }, + healthPoints: 10, + name: "John", + team: "Galaxy", + weapons: ["Missile", "Rockets"], + language: "Marxian" +}); + +const villian = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 3, + width: 3, + height: 5 + }, + healthPoints: 10, + name: "Tom", + team: "Jupiter", + weapons: ["Hammer", "Bombs"], + language: "Jupillian" +}); + +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!