-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
301 lines (285 loc) Β· 10.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
EXAMPLE TASK:
- Write an Airplane class whose constructor initializes `name` from an argument.
- All airplanes built with Airplane should initialize with an `isFlying` property of false.
- Give airplanes the ability to `.takeOff()` and `.land()`:
+ If a plane takes off, its `isFlying` property gets set to true.
+ If a plane lands, its `isFlying` property gets set to false.
*/
// EXAMPLE SOLUTION CODE:
class Airplane {
constructor(name) {
this.name = name;
this.isFlying = false;
}
takeOff() {
this.isFlying = true;
}
land() {
this.isFlying = false;
}
}
/*
// π COMPLETE YOUR WORK BELOW π
// π COMPLETE YOUR WORK BELOW π
// π COMPLETE YOUR WORK BELOW π
*/
/*
TASK 1
- Write a Person class whose constructor initializes `name` and `age` from arguments.
- All instances of Person should also initialize with an empty `stomach` array.
- Give instances of Person the ability to `.eat("someFood")`:
+ When eating an edible, it should be pushed into the `stomach`.
+ The `eat` method should have no effect if there are 10 items in the `stomach`.
- Give instances of Person the ability to `.poop()`:
+ When an instance poops, its `stomach` should empty.
- Give instances of Person a method `.toString()`:
+ It should return a string with `name` and `age`. Example: "Mary, 50"
*/
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
this.stomach = [];
}
eat(someFood) {
if (this.stomach.length < 10) {
this.stomach.push(someFood);
} else {
return "Sorry, but this person cannot eat anymore.";
}
}
poop() {
return (this.stomach.length = 0);
}
toString() {
return `${this.name}, ${this.age}`;
}
}
// Testing Person class w/ richard
const richard = new Person("Richard", 22);
richard.eat("Chicken Sandwich");
richard.eat("Green Apple");
console.log("Task 1:", richard);
/*
TASK 2
- Write a Car class whose constructor initializes `model` and `milesPerGallon` from arguments.
- All instances built with Car:
+ should initialize with a `tank` at 0
+ should initialize with an `odometer` at 0
- Give cars the ability to get fueled with a `.fill(gallons)` method. Add the gallons to `tank`.
- Give cars ability to `.drive(distance)`. The distance driven:
+ Should cause the `odometer` to go up.
+ Should cause the the `tank` to go down taking `milesPerGallon` into account.
- A car which runs out of `fuel` while driving can't drive any more distance:
+ The `drive` method should return a string "I ran out of fuel at x miles!" x being `odometer`.
*/
class Car {
constructor(model, milesPerGallon) {
this.model = model;
this.milesPerGallon = milesPerGallon;
this.tank = 0;
this.odometer = 0;
}
fill(gallons) {
this.tank += gallons;
}
drive(distance) {
const drivableMiles = this.tank * this.milesPerGallon;
// If I can't get there...
if (drivableMiles < distance) {
this.tank = 0;
this.odometer += drivableMiles;
return `I ran out of fuel at ${this.odometer} miles!`;
} else {
// If I can get there...
this.tank -= distance / this.milesPerGallon;
this.odometer += distance;
return `You traveled ${distance} miles!`;
}
}
}
const ford = new Car("Mustang", 25);
ford.fill(4);
console.log(ford.drive(101));
/*
TASK 3
- Write a Lambdasian class.
- Its constructor takes a single argument - an object with the following keys:
+ name
+ age
+ location
- Its constructor should initialize `name`, `age` and `location` properties on the instance.
- Instances of Lambdasian should be able to `.speak()`:
+ Speaking should return a phrase `Hello my name is {name}, I am from {location}`.
+ {name} and {location} of course come from the instance's own properties.
*/
class Lambdasian {
constructor(attributes) {
this.name = attributes.name;
this.age = attributes.age;
this.location = attributes.location;
}
speak() {
return `Hello my name is ${this.name}, I am from ${this.location}.`;
}
}
/*
TASK 4
- Write an Instructor class extending Lambdasian.
- Its constructor takes a single argument - an object with the following keys:
+ All the keys used to initialize instances of Lambdasian.
+ `specialty`: what the instance of Instructor is good at, i.e. 'redux'
+ `favLanguage`: i.e. 'JavaScript, Python, Elm etc.'
+ `catchPhrase`: i.e. `Don't forget the homies`.
- The constructor calls the parent constructor passing it what it needs.
- The constructor should also initialize `specialty`, `favLanguage` and `catchPhrase` properties on the instance.
- Instructor instances have the following methods:
+ `demo` receives a `subject` string as an argument and returns 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 returns '{student.name} receives a perfect score on {subject}'
*/
class Instructor extends Lambdasian {
constructor(instructorObj) {
super(instructorObj);
this.specialty = instructorObj.specialty;
this.favLanguage = instructorObj.favLanguage;
this.catchPhrase = instructorObj.catchPhrase;
}
demo(subject) {
return `Today we are learning about ${subject}`;
}
grade(studentObj, subject) {
return `${studentObj.name} receives a perfect score on ${subject}`;
}
gradeChange(studentObj) {
const oneOrTwo = Math.ceil(Math.random() * 2);
if (oneOrTwo === 1) {
studentObj.grade -= Math.ceil(Math.random() * 100);
} else {
studentObj.grade += Math.ceil(Math.random() * 100);
}
if (studentObj.grade < 0) {
return (studentObj.grade = 1);
} else if (studentObj.grade > 100) {
return (studentObj.grade = 100);
} else {
return studentObj.grade;
}
}
}
/*
TASK 5
- Write a Student class extending Lambdasian.
- Its constructor takes a single argument - an object with the following keys:
+ All the keys used to initialize instances of Lambdasian.
+ `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', 'JS']
- The constructor calls the parent constructor passing to it what it needs.
- The constructor should also initialize `previousBackground`, `className` and `favSubjects` properties on the instance.
- Student instances have the following methods:
+ `listSubjects` a method that returns all of the student's favSubjects in a single string: `Loving HTML, CSS, JS!`.
+ `PRAssignment` a method that receives a subject as an argument and returns `student.name has submitted a PR for {subject}`
+ `sprintChallenge` similar to PRAssignment but returns `student.name has begun sprint challenge on {subject}`
*/
class Student extends Lambdasian {
constructor(studentObj) {
super(studentObj);
this.previousBackground = studentObj.previousBackground;
this.className = studentObj.className;
this.favSubjects = studentObj.favSubjects;
this.grade = Math.ceil(Math.random() * 100);
}
listSubjects() {
return `Loving ${this.favSubjects}`;
}
PRAssignment(subject) {
return `${this.name} has submitted a PR for ${subject}`;
}
sprintChallenge(subject) {
return `${this.name} has begun sprint challenge on ${subject}`;
}
graduate() {
if (this.grade >= 70) {
return "Graduated";
} else if (this.grade < 70) {
return "Failed";
} else {
return "This student's test must be graded first.";
}
}
}
/*
TASK 6
- Write a ProjectManager class extending Instructor.
- Its constructor takes a single argument - an object with the following keys:
+ All the keys used to initialize instances of Instructor.
+ `gradClassName`: i.e. CS1
+ `favInstructor`: i.e. Sean
- Its constructor calls the parent constructor passing to it what it needs.
- The constructor should also initialize `gradClassName` and `favInstructor` properties on the instance.
- ProjectManager instances have the following methods:
+ `standUp` a method that takes in a slack channel and returns `{name} announces to {channel}, @channel standy times!`
+ `debugsCode` a method that takes in a student object and a subject and returns `{name} debugs {student.name}'s code on {subject}`
*/
class ProjectManager extends Instructor {
constructor(managerObj) {
super(managerObj);
this.gradClassName = managerObj.gradClassName;
this.favInstructor = managerObj.favInstructor;
}
standUp(slackChannel) {
return `${this.name} announces to ${slackChannel}, @channel standy times!`;
}
debugsCode(studentObj, subject) {
return `${this.name} debugs ${studentObj.name}'s code on ${subject}`;
}
}
/*
STRETCH PROBLEM (no tests!)
- 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.
*/
// Testing Stretch Goal
const oliver = {
name: "Oliver",
age: 27,
location: "California",
previousBackground: "Sports Writer",
className: "Web51",
favSubjects: ["JS", "Java", "CSS"],
};
const emily = {
name: "Emily",
age: 30,
location: "Texas",
specialty: "Backend Development",
favLanguage: "Java",
catchPhrase: "It is not over until it is over.",
};
const studentOliver = new Student(oliver);
const instructorEmily = new Instructor(emily);
// Stretch Goal Events
instructorEmily.gradeChange(studentOliver);
// Logging Stretch Goal
console.log(
`Oliver scored a ${studentOliver.grade}:`,
studentOliver.graduate()
);
//End of Challenge
/* ππππππππππ Please do not modify anything below this line ππππππππππ */
function foo() {
return "bar";
}
module.exports = {
foo,
Person,
Car,
Lambdasian,
Instructor,
Student,
ProjectManager,
};