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
100 lines (77 loc) · 1.99 KB
/
app.js
File metadata and controls
100 lines (77 loc) · 1.99 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
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(hobby){
this.hobbies.push(hobby);
}
removeHobby(hobby){
let foundIdx = this.hobbies.indexOf(hobby);
let valuesBefore = this.hobbies.slice(0, foundIdx);
let valuesAfter = this.hobbies.slice(foundIdx + 1);
this.hobbies = valuesBefore.concat(valuesAfter);
}
gretting(){
console.log("Hello fellow person!");
}
// override the Object.prototype to string method
toString(){
return Object.values(this).toString;
}
}
// 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("Hello, I am a coder!");
}
}
// Exercise 3 Section
console.log("EXERCISE 3:\n==========\n");
let coder = new Coder ("Ben", 0, "Birmingham", ["running", "diy", "reading"], "Full Stack Web Developer");
coder.addHobby("art");
coder.removeHobby("diy");
console.log(coder);
console.log(coder.toString());
// Exercise 4 Section
console.log("EXERCISE 4:\n==========\n");
class Calculator {
result = 0;
add(a, b){
this.result = a + b;
return this.result;
}
substract (a, b) {
this.result = a - b;
return this.result;
}
multiply (a, b) {
this.result = a * b;
return this.result;
}
divide (a, b) {
this.result = a / b;
return this.result;
}
display () {
console.log(this.result);
}
}
let cal = new Calculator ();
cal.add(0,1)
cal.display()
cal.substract(5, 3)
cal.display()
cal.multiply(1, 3)
cal.display()
dd