JSFiddle - React, Tailwind, and code Playground
by danbeam
JavaScript
function assert(cond) {
if (!cond)
throw 'fail';
console.log('ok');
}
function Node(data) {
this.id = ++Node.ID;
this.edges = [];
}
Node.ID = 0;
function isRouteTo(point, id) {
if (!point)
return false;
if (point.id == id)
return true;
for (var i = 0; i < point.edges.length; ++i) {
if (isRouteTo(point.edges[i], id))
return true;
}
return false;
}
var start = new Node;
start.edges.push(new Node);
start.edges.push(new Node);
start.edges.push(new Node);
start.edges[0].edges.push(new Node);
start.edges[0].edges[0].edges.push(new Node);
start.edges[1].edges.push(new Node);
start.edges[2].edges.push(new Node);
start.edges[2].edges[0].edges.push(new Node);
start.edges[2].edges[0].edges[0].edges.push(new Node);
assert(isRouteTo(start, 1));
assert(isRouteTo(start, Math.floor(Node.ID / 2)));
assert(isRouteTo(start, Node.ID));
assert(!isRouteTo(start, Node.ID + 1));