-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjune17exercise.js
55 lines (47 loc) · 1.63 KB
/
june17exercise.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
// Create a Location class with a name.
class Location {
constructor(name) {
this.name = name;
}
}
// Create a Trip class with a list of Location instances (called stops or destinations or something similar).
// Define a method that lets you add locations to the trip's list of destinations.
// Define a method in the Trip class that iterates through the list of locations and prints something similar to the following:
// "Began trip."
// "Travelled from Toronto to Ottawa."
// "Travelled from Ottawa to Montreal."
// "Travelled from Montreal to Quebec City."
// "Travelled from Quebec City to Halifax."
// "Travelled from Halifax to St. John's."
// "Ended trip."
class Trip {
constructor() {
this.destinations = []
}
addLocation(location) {
this.destinations.push(location);
}
tripSummary() {
var i;
console.log('Began trip.')
for (i=0; i<(this.destinations.length - 1); i++) {
console.log('Travelled from ' + this.destinations[i].name + ' to ' + this.destinations[i+1].name + '.')
}
console.log('Ended trip.')
}
}
// Make several instances of Locations and add them to an instance of Trip.
const toronto = new Location('Toronto')
const ottawa = new Location('Ottawa')
const montreal = new Location('Montreal')
const quebecCity = new Location('Quebec City')
const halifax = new Location('Halifax')
const stJohns = new Location("St John's")
myTrip = new Trip()
myTrip.addLocation(toronto)
myTrip.addLocation(ottawa)
myTrip.addLocation(montreal)
myTrip.addLocation(quebecCity)
myTrip.addLocation(halifax)
myTrip.addLocation(stJohns)
myTrip.tripSummary()