forked from ga-wolf/WDI13_Homework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MTA2.js
113 lines (100 loc) · 3.24 KB
/
MTA2.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
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
var routes = [
{
name: 'N',
stops:['Time Square', '34th Street', '28th Street', '23rd Street', 'Union Square', '8th Street']
},
{
name: 'L',
stops: ['8th Street', '6th Street', 'Union Square', '3rd Street', '1st Street']
},
{
name: '6',
stops: ['Grand Central', '33rd Street', '28th Street', '23rd Street', 'Union Square', 'Astor Place']
}
];
var u = 0; // index of startingStation
var v = 0; // index of Union Square on that line
var w = 0; // index of starting Line
var x = 0; // index of endStation
var y = 0; // index of Union Sqaure on that Line
var z = 0; // index of ending line
var startLeg = [];
var endLeg = [];
var planRoute = function(startLine, startingStation, endLine, endStation){
getStart(startLine, startingStation);
getEnd(endLine, endStation);
getStartLeg();
getEndLeg();
if (w === z){
console.log('Start at ' + startingStation + ' on the ' +
startLine + ' Line, through ' + startLeg + ' and get off at ' + endStation);
}
else if (u - v === 1 || v + 1 === u){
console.log('Start at ' + startingStation + ' change at ' + ' Union Square ' +
' continue through to line ' + endLine + ' and get off at ' + endStation);
}
else {
console.log('Start at ' + startingStation + ' on line ' + startLine + ' through stations '
+ startLeg + ' change at Union Sqaure then proceed through to ' + endLeg +
' and get off at ' + endStation + ' on line ' + endLine);
}
};
/* loops through the routes array objects and sets the index of the given
startLine to var w & the index of Union Sqaure on this line to var y
*/
var getStart = function(startLine, startingStation){
for (var i = 0; i < routes.length; i++){
if (startLine === routes[i].name){
for (var j = 0; j < routes[i].stops.length; j++){
if (startingStation === routes[i].stops[j]){
u = routes[i].stops.indexOf(startingStation);
v = routes[i].stops.indexOf('Union Square');
w = i;
}
}
}
}
};
/* loops through the routes array objects and sets the index of the given
endLine to var x & the index of Union Square on this line to var z
*/
var getEnd = function(endLine, endStation){
for (var i = 0; i < routes.length; i++){
if (endLine === routes[i].name){
for (var j = 0; j < routes[i].stops.length; j++){
if (endStation === routes[i].stops[j]){
x = routes[i].stops.indexOf(endStation);
y = routes[i].stops.indexOf('Union Square');
z = i;
}
}
}
}
};
var getStartLeg = function (){
if (u < v){ //u = starting station , v = union square
for (var i = u + 1; i < v; i++){
startLeg.push(routes[w].stops[i]);
}
} else if (v < u){
for (var j = u - 1; j > v; j--){
startLeg.push(routes[w].stops[j]);
}
}
else {
for (var k = u + 1; k < y; k++){
startLeg.push(routes[w].stops[k]);
}
}
};
var getEndLeg = function (){
if (x < y){ //x = end station station , y = union square
for (var i = y - 1; i > x; i--){
endLeg.push(routes[z].stops[i]);
}
} else {
for (var j = y + 1; j < x; j++){
endLeg.push(routes[z].stops[j]);
}
}
};