Reputation: 41
I get a syntax error: unexpected identifier at line 22. I've been over and over this code, and I can't for the life of me figure out what's wrong. It's a code to determine the shortest route from one node to another.
"use strict"
function findpath(G,si,di){
//G is an array of nodes (with id, lat, lon)
var cvi = si;
var P=[si];
var C=[0,P];
var M=[C];
var O=[];
var ctr=0;
var done = false;
var reached = false;
var best = undefined;
while(!done){
ctr++;
if( ctr > 100 ){
alert("Sorry, can't find the destination.");
return P;
}
for (int i=0;i<M.length;++i){
var last = M[i[1]].length;
var v = M[i[1[last]]];
//select a random neighbor...
if( v.N.length === 0 ){
alert("Wat?");
return [];
}
else if( v.N.length === 1 ){
break;
}
else if( v === di ){
break;
}
else {
for (int j=0;j<v.N.length;++j){
var temp = M[i];
O.push(temp[1].push(v.N[j]));
var dist = distance(v.lat,v.lon,v.N[j].lat,v.N[j].lon);
var temp2 = O.length-1;
O[temp2[0]]+=dist;
if (v.N[j]===di){
reached = true;
if (best === undefined){
console.log("ASSIGN");
best = O[temp2];
}
else {
if (O[temp2[0]]<best[0]) {
best = O[temp2];
}
}
}
}
}
}
M = O;
var any = false;
for (int i=0;i<M.length;++i) {
if (M[i[0]]<best[0]) {
any = true;
}
}
if (!any) {
done = true;
}
}
//return the path
return best[1];
}
function distance(x1,y1,x2,y2){
return Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2));
}
Upvotes: 1
Views: 7024
Reputation: 48761
This:
for (int i=0;i<M.length;++i){
shouldn't have int
. It should be var
.
for (var i=0;i<M.length;++i){
Upvotes: 9
Reputation: 943220
You have for (int i=0;i<M.length;++i){
.
int
is an identifier, but not one that is part of JavaScript.
You probably meant var
.
Upvotes: 3