I am practising coding on codewars.com. I came across this question:
We are tracking down our rogue agent Matthew Knight A.K.A. Roy Miller and he travels from places to places to avoid being tracked. Each of his travels are based on a list of itineraries in an unusual or incorrect order. The task is to determine the routes he will take in his every journey. You are given an array of routes of his itineraries. List down only the places where he will go in correct order based on his itineraries.
Example: routes =
[[USA, BRA], [JPN, PHL], [BRA, UAE], [UAE, JPN]]result:
"USA, BRA, UAE, JPN, PHL"
I have tried with the code below:
function findRoutes(routes) {
let newArr = Array.prototype.concat.apply([], routes)
let unique = newArr.filter((v, i, a) => a.indexOf(v) === i);
return unique.join(', ');
}
console.log(findRoutes([["USA","BRA"],["JPN","PHL"],["BRA","UAE"],["UAE","JPN"]]))
It only worked for the case given but when I tried aginst another test case:
[["Chicago", "Winnipeg"], ["Halifax", "Montreal"], ["Montreal", "Toronto"], ["Toronto", "Chicago"], ["Winnipeg", "Seattle"]]
If failed...Therefore I knew something is wrong since the movement should be orderly. I would be glad to improve it or rewrite from a different idea.

