-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodewarsChallenge59.js
More file actions
65 lines (51 loc) · 1.38 KB
/
codewarsChallenge59.js
File metadata and controls
65 lines (51 loc) · 1.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
// Super = keyword is used in classes to call the constructor or
// access the properties and methose of a parent(supperClass)
// this = this object
// super = the parent
class Animal{
constructor(name, age){
this.name = name;
this.age = age
}
move(speed){
console.log(`The ${this.name} moves at a speed of ${speed}mph`)
}
}
class Rabbit extends Animal{
constructor(name, age, runSpeed){
super(name, age);
this.runSpeed = runSpeed;
}
run(){
console.log(`This ${this.name} can run`);
super.move(this.runSpeed);
}
}
class Fish extends Animal{
// Calls the parent class constructor to initialize name and age properties.
constructor(name, age, swimSpeed){
super(name, age);
this.swimSpeed = swimSpeed;
}
swim(){
console.log(`This ${this.name} can swim`);
super.move(this.swimSpeed);
}
}
class Hawk extends Animal{
constructor(name, age, flySpeed){
super(name, age);
this.flySpeed = flySpeed;
}
fly(){
console.log(`This ${this.name} can fly`);
super.move(this.flySpeed);
}
}
const rabbit = new Rabbit ("rabbit", 1, 25)
const fish = new Fish ("fish", 1, 25)
const hawk = new Hawk ("hawk", 3, 50)
console.log(rabbit.name);
console.log(rabbit.age)
console.log(rabbit.runSpeed)
rabbit.run();