forked from DevMountain/javascript-3-afternoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.js
More file actions
202 lines (169 loc) · 5.68 KB
/
classes.js
File metadata and controls
202 lines (169 loc) · 5.68 KB
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
/*
Once you complete a problem, refresh ./classes.html in your browser and check to see if the problem's test(s) are passing.
Passed tests will be indicated by a green circle.
Failed tests will be indicated by a red X.
You can refresh the page at any time to re-run all the tests.
Classes are a tool for building similar objects over and over again.
They are a construct that helps your organize your code.
Let's work with some employees at a company.
You work for Widget Co. They have hundreds of employees.
*/
////////// PROBLEM 1 //////////
/*
Make a class to help us build all of the employees.
Each employee has the following properties:
- first_name
- last_name
- email
- age
Each employee has the following methods:
- makeWidget
- This returns a string equal to the employees first name + last name + the word widget
- Example: "Dave Smith Widget"
Call your class Employee and receive all the data in the constructor in the order listed above.
*/
//Code Here
class Employee {
constructor(first_name, last_name, email, age) {
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.age = age;
}
makeWidget(){
return `${this.first_name} ${this.last_name} Widget`;
}
}
const employee1 = new Employee('Michael', 'Jackson', 'email@domain', 42);
const employee2 = new Employee('Steven', 'Colbert', 'email@domain', 51);
// console.log(employee1.makeWidget());
////////// PROBLEM 2 //////////
/*
Next, make a manager for Widget Co.
The manager has all the same properties as an Employee. Copy the Employee class and rename it Manager.
Each manager has the following additional properties:
- reports (other employees) that defaults to an empty array
Each manager has the following additional methods:
- hire (employee)
- Accepts a new employee as a parameter and pushes it to their list of reports.
- fire (index)
- Fire removes employees from their list of reports at the given index
Call your new class Manager
*/
//Code Here
class Manager {
constructor(first_name, last_name, email, age) {
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.age = age;
this.reports = [];
}
makeWidget(){
return `${this.first_name} ${this.last_name} Widget`;
}
hire(employee){
this.reports.push(employee);
}
fire(employeeIndex){
// this.reports.splice(this.reports.indexOf(employee), 1);
this.reports.splice(employeeIndex, 1);
}
}
// const manager = new Manager('David', 'Smith', 'email@domain', 31)
// manager.hire(employee1);
// manager.hire(employee2);
// console.log(manager);
// manager.fire(employee2);
// console.log(manager);
////////// PROBLEM 3 //////////
/*
Managers for Widget Co. get promoted when they get more employees, and get a bonus when they fire employees.
Progressive managers have all the same properties as managers. Copy the Manager class and rename
it to ProgressiveManager. Add the following additional properties:
- title - default 'Not a manager'
- bonus - default 0
When employees are hired or fired, the manager's title should be updated based on the number of reports.
0 reports : Not a manager
1-3 reports : Barely Manager
4-10 reports : Mostly Manager
11-50 reports : Manager
51-100 reports : Manager Plus
101+ reports : Bestest Manager
Everytime they fire an employee they get $100 added to their bonus.
Call your new class ProgressiveManager
*/
//Code Here
class ProgressiveManager {
constructor(first_name, last_name, email, age) {
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.age = age;
this.reports = [];
this.title = 'Not a manager';
this.bonus = 0;
}
makeWidget(){
return `${this.first_name} ${this.last_name} Widget`;
}
hire(employee){
this.reports.push(employee);
const reports = this.reports.length;
reports >= 1 && reports <= 3
? this.title = 'Barely Manager'
: reports >= 4 && reports <= 10
? this.title = 'Mostly Manager'
: reports >= 11 && reports <= 50
? this.title = 'Manager'
: reports >= 51 && reports <= 100
? this.title = 'Manager Plus'
: this.title = 'Bestest Manager'
}
fire(employeeIndex){
// this.reports.splice(this.reports.indexOf(employee), 1);
this.reports.splice(employeeIndex, 1);
this.bonus += 100;
}
}
////////// PROBLEM 4 - Black Diamond //////////
/*
Widget Co has a factory that makes widgets.
Factories have Machines.
Make a Machine class that takes in no parameters
A Machine has the following properties:
- widgets_made_count - default 0
- wear_and_tear_count - default 0
- needs_reboot - default false
A Machine has the following methods:
- makeWidgets
- This function takes in a number and increases widgets_made_count by that amount
- It also increases wear_and_tear_count by 1 for every 50
- fixMachine
- This function sets needs_reboot to true
- reboot
- This function returns an anonymous function that is called when the machine is done rebooting
- The anonymous function should decrease wear_and_tear_count by 10, and set needs_reboot to false
*/
//Code Here
class Machine {
constructor() {
this.widgets_made_count = 0;
this.wear_and_tear_count = 0;
this.needs_reboot = false;
}
makeWidgets(num) {
this.widgets_made_count += num;
const wtCount = num / 50;
this.wear_and_tear_count += wtCount;
}
fixMachine() {
this.needs_reboot = true;
}
reboot() {
return () => {
this.wear_and_tear_count -= 10;
this.needs_reboot = false;
}
}
}