"use strict";
function app(){
var stack = [];
var one = new Ticket('London', 'NYC');
var two = new Ticket('Barcelona', 'Athens');
var three = new Ticket('Rio', 'ND');
var four = new Ticket('NYC', 'Barcelona');
var five = new Ticket('Athens', 'Rio');
var six = new Ticket('ND', "Lahore");
stack.push(one);
stack.push(two);
stack.push(three);
stack.push(four);
stack.push(five);
stack.push(six);
var res = sortDestinations(stack, 'London', 'Lahore');
for(var city in res){
console.out(res[city]);
}
}
function Ticket(departure, destination){
this.departure = departure;
this.destination = destination;
}
Ticket.prototype.getDeparture = function(){
return this.departure;
}
Ticket.prototype.getDestination = function(){
return this.destination;
}
function sortDestinations(stack, dep, dest){
var map = {};
for(var i= 0; i<stack.length; i++){
var ticket = stack.pop();
map[ticket.getDeparture()] = ticket.getDestination();
}
var res = [];
var curr = dep;
res.push(curr);
while(true){
if(curr == dest) {
break;
}
var next = map[curr];
res.push(next);
curr = next;
}
}
app();