Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ifThereIsRouteFrom(start) {
- const queue = [start];
- return {
- to(finish) {
- currentCity = queue.shift();
- if (typeof currentCity === "undefined") return false;
- for (let i = 0, len = currentCity.length; i < len; i++) {
- if (currentCity[i]['name'] === finish) return true;
- queue.push(currentCity[i]);
- }
- return this.to(finish);
- }
- }
- }
- /**
- this is how routes object should look like
- const routes = {
- 0: {
- 0: {
- 0: {
- 0: {
- name: 'helsinki',
- length: 0
- },
- name: 'sp',
- length: 1
- },
- 1: {
- 0: {
- name: 'chelyabinsk',
- length: 0
- },
- 1: {
- name: 'vladivostok',
- length: 0
- },
- name: 'tumen',
- length: 2
- },
- 2: {
- name: 'krasnodar',
- length: 0
- },
- name: 'kazan',
- length: 3
- },
- 1: {
- 0: {
- name: 'krasnodar',
- length: 0
- },
- 1: {
- name: 'erevan',
- length: 0
- },
- name: 'sochi',
- length: 2
- },
- 2: {
- 0: {
- name: 'erevan',
- length: 0
- },
- 1: {
- 0: {
- name: 'minsk',
- length: 0
- },
- name: 'kyiv',
- length: 1
- },
- name: 'smolensk',
- length: 2
- },
- name: 'moscow',
- length: 3
- }
- }
- **/
- // Example of usage
- // const startPoint = routes[0];
- // console.log(ifThereIsRouteFrom(startPoint).to('vladivostok'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement