forked from ga-wdi-boston/wdi_7_js_hw_mbta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbta.js
46 lines (33 loc) · 1.29 KB
/
mbta.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
// Code here.
var startingLine,
startingStation,
endingLine,
endingStation;
/*
startingLine = prompt("Enter the Starting Line: ");
startingStation = prompt("Enter the Starting Station");
endingLine = prompt("Enter the Ending Line");
endingStation = prompt("Enter the Ending Station");
alert("Staring at " + startingLine + " : " + startingStation);
alert("Ending at " + endingLine + " : " + endingStation);
*/
// Create a array for each line
var lines = {
'green':['haymarket', 'government center', 'park st', 'bolyston', 'arlington', 'copley'],
'red': ['south station', 'park st', 'kendall', 'central', 'harvard', 'porter', 'davis', 'alewife'],
'orange': ['north station', 'haymarket', 'park st', 'state', 'downtown crossing', 'chinatown', 'back bay', 'forest hills']
};
function stopsOnOneLine(line, start, end) {
return Math.abs(lines[line].indexOf(start) - lines[line].indexOf(end));
}
function trip(startLine, startStation, endLine, endStation) {
var stops = 0;
if (startLine === endLine) {
stops = stopsOnOneLine(startLine, startStation, endStation);
} else {
stops = stopsOnOneLine(startLine, startStation, 'park st') +
stopsOnOneLine(endLine, endStation, 'park st');
}
return stops;
}
console.log("Stops: " + trip('green', 'copley', 'orange', 'forest hills'));