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
112 lines (96 loc) · 2.38 KB
/
app.js
File metadata and controls
112 lines (96 loc) · 2.38 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
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(hobby) {
this.hobbies.push(hobby);
}
removeHobby(hobbyToRemove) {
// Using filter to create a new array without the specified hobby
this.hobbies = this.hobbies.filter(element => element !== hobbyToRemove);
}
greeting() {
console.log("Hello World!");
}
}
// Exercise 3
const person = new Person("Sirius Black", ["cat"], "Grimold place", ["sulks", "advises", "inspires", "hounds"]);
console.log(person);
person.addHobby("kayaking");
person.removeHobby("kayaking");
console.log(person);
person.greeting();
// Exercise 2 & 3
class Coder extends Person {
constructor(name, pets, residence, hobbies) {
super(name, pets, residence, hobbies);
this.occupation = "Full Stack Web Developer";
}
greeting() {
console.log("Hello World");
}
}
// Exercise 3
const coder = new Coder("Wade", ["dog"], "162 Fake Street", ["gaming", "hacking", "surfing"]);
console.log(coder);
coder.greeting();
// Exercise 4
class Calculator {
constructor() {
this.result = 0;
}
add(a, b) {
if (b === undefined) {
this.result = this.result + a;
} else {
this.result = a + b;
}
return this.result;
}
subtract(a, b) {
if (b === undefined) {
this.result = this.result - a;
} else {
this.result = a - b;
}
return this.result;
}
multiply(a, b) {
if (b === undefined) {
this.result = this.result * a;
} else {
this.result = a * b;
}
return this.result;
}
divide(a, b) {
if (b === undefined) {
this.result = this.result / a;
} else {
this.result = a / b;
}
return this.result;
}
displayResult() {
console.log(this.result);
}
}
const calc = new Calculator();
calc.add(5, 7);
calc.displayResult();
calc.subtract(3, 5);
calc.displayResult();
calc.multiply(2, 7);
calc.displayResult();
calc.divide(6, 5);
calc.displayResult();
calc.add(0.8);
calc.displayResult();
calc.subtract(1);
calc.displayResult();
calc.divide(5);
calc.displayResult();