Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. function ifThereIsRouteFrom(start) {
  2. const queue = [start];
  3. return {
  4. to(finish) {
  5. currentCity = queue.shift();
  6. if (typeof currentCity === "undefined") return false;
  7. for (let i = 0, len = currentCity.length; i < len; i++) {
  8. if (currentCity[i]['name'] === finish) return true;
  9. queue.push(currentCity[i]);
  10. }
  11. return this.to(finish);
  12. }
  13. }
  14. }
  15.  
  16. /**
  17.  
  18. this is how routes object should look like
  19.  
  20. const routes = {
  21. 0: {
  22. 0: {
  23. 0: {
  24. 0: {
  25. name: 'helsinki',
  26. length: 0
  27. },
  28. name: 'sp',
  29. length: 1
  30. },
  31. 1: {
  32. 0: {
  33. name: 'chelyabinsk',
  34. length: 0
  35. },
  36. 1: {
  37. name: 'vladivostok',
  38. length: 0
  39. },
  40. name: 'tumen',
  41. length: 2
  42. },
  43. 2: {
  44. name: 'krasnodar',
  45. length: 0
  46. },
  47. name: 'kazan',
  48. length: 3
  49. },
  50. 1: {
  51. 0: {
  52. name: 'krasnodar',
  53. length: 0
  54. },
  55. 1: {
  56. name: 'erevan',
  57. length: 0
  58. },
  59. name: 'sochi',
  60. length: 2
  61. },
  62. 2: {
  63. 0: {
  64. name: 'erevan',
  65. length: 0
  66. },
  67. 1: {
  68. 0: {
  69. name: 'minsk',
  70. length: 0
  71. },
  72. name: 'kyiv',
  73. length: 1
  74. },
  75. name: 'smolensk',
  76. length: 2
  77. },
  78. name: 'moscow',
  79. length: 3
  80. }
  81. }
  82.  
  83. **/
  84.  
  85. // Example of usage
  86. // const startPoint = routes[0];
  87. // console.log(ifThereIsRouteFrom(startPoint).to('vladivostok'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement