title: Breadth First Search in Javascript
body: <pre><code>function BFS_search(data, start, ende){
      var queue = new Array();
      var steps = 0;
      for(var x=0; x&lt;data.Adjazenzen.length-1; x++){  //push start Element in Queue
          if(data.Adjazenzen[x] == start){
              queue.push(data.Adjazenzen[x]);
              steps++;
              break;
          }
      }
  
      while(queue.length != 0) {
          var t = queue.pop();
          if(t.Von == ende) {
              return t.Von;
          }
          if(t.Nach == ende){
              return t.Nach;
          }
          else{
              queue.push(data.Adjazenzen[0]);
              data.Adjazenzen.shift();
              steps++;
              break;
          }
      }
      return "keine Ergebnis";
  }
  </code></pre>
  
  <p>This code snippet should calculate the best route between two points from a given JSON Object and also add all Points between the start and end node. Basically it should be a navigation-system using BFS.</p>
  
  <p>The JSON Object looks like this:</p>
  
  <pre><code>{"Adjazenzen":[
  {"Von":"A1.02","Nach":"G-A3-1","X":"5778","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"A1.04","Nach":"G-A3-1","X":"5025","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"A1.05","Nach":"G-A3-1","X":"4212","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"A1.06","Nach":"G-A3-1","X":"3016","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"A1.07","Nach":"G-A3-1","X":"2354","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"A1.08","Nach":"G-A3-1","X":"1856","Y":"223","Z":"3","Treppe":"1","Gebaeude":"A","Level":"0"},
      {"Von":"A1.12","Nach":"G-A3-1","X":"1313","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"B1.01","Nach":"G-B3-1","X":"6572","Y":"223","Z":"3","Treppe":"1","Gebaeude":"B","Level":"0"},
      {"Von":"G-A3-1","Nach":"G-A3-2","X":"906","Y":"223","Z":"3","Treppe":{},"Gebaeude":{},"Level":{}},
      {"Von":"G-A3-1","Nach":"B1.01","X":"6572","Y":"223","Z":"3","Treppe":"1","Gebaeude":"B","Level":"0"},
      {"Von":"G-A3-2","Nach":"A1.14","X":"1066","Y":"1333","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"G-A3-2","Nach":"G-A3-3","X":"1444","Y":"4016","Z":"3","Treppe":{},"Gebaeude":{},"Level":{}},
      {"Von":"G-A3-2","Nach":"A1.15","X":"1207","Y":"2340","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"G-A3-2","Nach":"A1.18","X":"1444","Y":"4016","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"G-A3-2","Nach":"A1.16","X":"1327","Y":"3188","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"G-A3-2","Nach":"A1.13","X":"933","Y":"383","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"G-A3-2","Nach":"A1.17","X":"1425","Y":"3884","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"G-A3-3","Nach":"A1.19","X":"2691","Y":"3977","Z":"3","Treppe":"1","Gebaeude":"A","Level":"0"},
      {"Von":"G-A3-3","Nach":"A1.22","X":"2946","Y":"3969","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"G-A3-3","Nach":"A1.23","X":"2946","Y":"3969","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
      {"Von":"G-A3-3","Nach":"A1.20","X":"1990","Y":"4002","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"}]}
  </code></pre>
  
  <p>You can ignore everything except Von (From) and Nach (To).
  The problem is that my BFS is not 100% working and I don't know how to fix it.</p>
  
  <p>An Example:
  If you want to calculate from A1.04 to A1.15 it should add:
  A1.04, G-A3-1, G-A3-2, A1.15</p>
  
  <p>And if you want from A1.04 to A1.20:
  A1.04, G-A3-1, G-A3-2, G-A3-3, A1.20</p>
  
  <p>I hope you can help me to solve this problem.</p>