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
119 lines (108 loc) · 2.34 KB
/
app.js
File metadata and controls
119 lines (108 loc) · 2.34 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
console.log("Hello World!\n==========\n");
function exercise(n) {
console.log(`EXERCISE ${n}:\n==========\n`);
}
exercise(1);
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 i = this.hobbies.indexOf(hobby);
this.hobbies.splice(i, 1);
this.oldHobby = hobby;
}
greeting() {
console.log(`Hello World! My Name is ${this.name}.`);
}
}
exercise(2);
class Coder extends Person {
constructor(name, pets, residence, hobbies) {
super(name, pets, residence, hobbies);
this.occupation = "Full Stack Web Developer";
}
greeting() {
console.log(`function greeting(mood) {
if (mood == friendly) {
console.log("Hello, Friend! My Name is ${this.name}");
} else {
console.log("Hi.")
}
}`);
}
}
exercise(3);
let nicolas = new Person("Nicolas", 3, "Birmingham, AL", [
"Dungeons & Dragons",
"Board Games",
]);
console.log(nicolas.greeting());
console.log(nicolas);
nicolas.addHobby("Woodworking");
console.log(nicolas);
nicolas.removeHobby("Board Games");
console.log(nicolas);
let john = new Coder("John", 0, "Birmingham, AL", [
"Long Walks on the Beach",
"Eating Oreo Cookies",
]);
console.log(john);
john.addHobby("Warzone");
console.log(john);
john.removeHobby("Eating Oreo Cookies");
console.log(john);
console.log(john.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;
}
}
subtract(a, b) {
if (b == undefined) {
this.result = this.result - a;
} else {
this.result = a - b;
}
}
multiply(a, b) {
if (b == undefined) {
this.result = this.result * a;
} else {
this.result = a * b;
}
}
divide(a, b) {
if (b == undefined) {
this.result = this.result / a;
} else {
this.result = a / b;
}
}
displayResult() {
console.log(this.result);
}
}
let n = new Calculator();
n.displayResult();
n.add(149873, 13498);
n.displayResult();
n.subtract(13498);
n.displayResult();
n.multiply(149873, 13498);
n.displayResult();
n.divide(149873);
n.displayResult();