-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
59 lines (52 loc) · 1.24 KB
/
script.js
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
class Ship {
constructor(itinerary) {
this.itinerary = itinerary;
this.currentPort = itinerary.ports[0];
this.previousPort = null;
this.remainingPort = [...itinerary.ports];
this.currentPort.addShip(this);
}
setSail() {
if (this.remainingPort.length < 2) {
throw new Error("End of Itinerary reached");
} else {
this.currentPort.removeShip(this);
this.previousPort = this.currentPort;
this.currentPort = null;
this.remainingPort.shift();
}
}
dock() {
this.currentPort = this.remainingPort[0];
this.currentPort.addShip(this);
}
get nextPort() {
let itin = this.itinerary.ports;
let nextPortIndex = itin.indexOf(this.currentPort);
return itin[nextPortIndex + 1];
}
}
class Port {
constructor(name) {
this.name = name;
this.dockedShips = [];
}
addShip(ship) {
this.dockedShips.push(ship);
}
removeShip(ship) {
this.dockedShips = this.dockedShips.filter((e) => e !== ship);
}
}
class Itinerary {
constructor(ports) {
this.ports = ports;
}
}
if (typeof module !== "undefined" && module.exports) {
module.exports = { Ship, Port, Itinerary };
} else {
window.Port = Port;
window.Ship = Ship;
window.Itinerary = Itinerary;
}