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
127 lines (109 loc) · 2.97 KB
/
app.js
File metadata and controls
127 lines (109 loc) · 2.97 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
console.log("Hello World!\n==========\n");
// Exercise 1 Section
console.log("EXERCISE 1:\n==========\n");
class Person {
constructor(name, petCount, residence, hobbies) {
this.name = name;
this.petCount = petCount;
this.residence = residence;
this.hobbies = hobbies;
}
addHobby(newHobby){
this.hobbies.push(newHobby);
}
removeHobby(remHobby){
this.hobbies = this.hobbies.filter((element) => {
if (element != remHobby){
return true;
} else {
return false;
}
});
}
greeting(){
console.log("Hello fellow person!");
}
info() {
console.log(`Name: ${this.name}`);
console.log(`Pets: ${this.pets}`);
console.log(`Residence: ${this.residence}`);
console.log(`Hobbies: ${this.hobbies}`);
}
soundOff() {
console.log(`${this.name} is ready to sound off!`);
}
}
const personTest = new Person("Newt Skimandr", 200, "Suitcase", ["Zoology", "Magic", "Snacks"]);
personTest.addHobby("Traveling");
personTest.removeHobby("Snacks");
console.log(personTest.hobbies);
// Exercise 2 Section
console.log("EXERCISE 2:\n==========\n");
class Coder extends Person {
constructor(name, petCount, residence, hobbies){
super();
this.occupation = "Full Stack Web Developer";
}
//overriding
greeting(){
console.log("Hello fellow wizard!");
}
}
const coderTest = new Coder("Gandolf", 0, "Minas Tirith", ["Smoking", "Magic", "Hanging out at the Shire", "Blocking bridges"]);
coderTest.greeting();
coderTest.addHobby("feeding eagles");
console.log(coderTest.hobbies);
// Exercise 4 Section
console.log("EXERCISE 4:\n==========\n");
class Calculator {
constructor() {
this.result = 0;
}
add(a, b = this.result) {
if (b == undefined){
this.result += a;
} else {
this.result = a + b;
}
return this.result;
}
subtract(a, b = this.result) {
if (b == undefined){
this.result -= a;
} else {
this.result = a - b;
}
return this.result;
}
multiply(a, b = this.result) {
if (b == undefined){
this.result *= a;
} else {
this.result = a * b;
}
return this.result;
}
divide(a, b = this.result) {
if (b == undefined) {
if (a == undefined){
return null;
} else {
this.resuult = a / b;
return this.result;
}
}
}
}
displayResult();{
console.log(`Result: ${this.result}`);
}
const calcTest = new Calculator();
calcTest.displayResult();
calcTest.add(5, 1);
calcTest.displayResult();
calcTest.subtract(5, 1);
calcTest.displayResult();
calcTest.multiply(5, 2);
calcTest.displayResult();
calcTest.divide(5, 0);
calcTest.displayResult();