Skip to content

Instantly share code, notes, and snippets.

@ungarson
Created June 9, 2019 06:02
Show Gist options
  • Save ungarson/ae8041493048e5d6ea40dd5fe1e12417 to your computer and use it in GitHub Desktop.
Save ungarson/ae8041493048e5d6ea40dd5fe1e12417 to your computer and use it in GitHub Desktop.
A function that tells you if there is a nested object with specific name
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'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment