forked from truecodersio/JavaScript_OOP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
128 lines (108 loc) · 2.46 KB
/
app.js
File metadata and controls
128 lines (108 loc) · 2.46 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
console.log("Hello World!\n==========\n");
// Exercise 1 Section
console.log("EXERCISE 1:\n==========\n");
class Person {
constructor(name, pets, residence, hobbies) {
this.name = name;
this.pets = pets;
this.residence = residence;
this.hobbies = hobbies;
}
addHobby(newHobby) {
return this.hobbies.push(newHobby);
}
removeHobby(oldHobby) {
this.hobbies = this.hobbies.filter((ele) => ele !== oldHobby);
return this.hobbies;
}
greeting() {
console.log(`Good day, ${this.name}. I also enjoy ${this.hobbies[0]}. `);
}
}
// Exercise 2 Section
console.log("EXERCISE 2:\n==========\n");
class Coder extends Person {
constructor(name, pets, residence, hobbies, occupation) {
super(name, pets, residence, hobbies);
this.occupation = occupation;
}
greeting() {
console.log(
`Hey ${this.name}! I am a ${this.occupation} too and I also have ${this.pets} pets.`
);
}
}
// Exercise 3 Section
console.log("EXERCISE 3:\n==========\n");
const personOne = new Person(`Chris`, 2, `house`, [
`magnets`,
`milksteak`,
`ghouls`,
]);
personOne.addHobby(`coding`);
personOne.removeHobby(`ghouls`);
personOne.greeting();
console.log(personOne);
const coderOne = new Coder(
`Adam`,
1,
`tent`,
[`camping`, `fire`, `hard drugs`],
`junior developer`
);
coderOne.addHobby(`coding`);
coderOne.removeHobby(`fire`);
coderOne.greeting();
console.log(coderOne);
// Exercise 4 Section
console.log("EXERCISE 4:\n==========\n");
class Calculator {
constructor() {
this.result = 0;
}
// dammit!! checkParams(paramA, paramB) {
// if (paramB == undefined) {
// paramB = paramA;
// paramA = this.result;
// }
// return [paramA, paramB];
// }
add(a, b) {
if (b == undefined) {
b = a;
a = this.result
}
this.result = a + b;
return this.result;
}
subtract(a, b) {
if (b == undefined) {
b = a;
a = this.result
}
this.result = a - b;
return this.result;
}
multiply(a, b) {
if (b == undefined) {
b = a;
a = this.result
}
this.result = a * b;
return this.result;
}
divide(a, b) {
if (b == undefined) {
b = a;
a = this.result
}
this.result = a / b;
return this.result;
}
}
let newObj = new Calculator();
console.log(newObj.add(2, 5));
console.log(newObj.subtract(2, 5));
console.log(newObj.multiply(2, 5));
console.log(newObj.divide(2, 5));
console.log(newObj.add(5));