以下代码假设获取一个起始位置并在其周围创建一个其他位置的数组,并且它与它们的距离。我尝试调试,但崩溃发生在代码执行的最初阶段(在最新的chrome和firefox上)。
function makeRoads(){
try {
if(arguments.length%2 == 0){throw "you need to specify bla!";}
else {
var origin = arguments[0]
for (var i = 1; i < arguments.length; i+2) {
var destenation = arguments[i];
var distance = arguments[i+1];
makeRoad(origin, destenation, distance);
}
}
}
catch (error){
console.log(error);
}
finally{
console.log("fianlly!!");
}
}
function makeRoad(origin, destenation, distance) {
function addRoad(origin, destenation) {
if (!(origin in roads)){roads[origin] = [];}
roads[origin].push({to: destenation, distance: distance});
}
addRoad(origin, destenation);
addRoad(destenation, origin);
}
我使用makeRoads("a"/*the origin*/,"b",3/*first destination and distance*/,"c",4)
答案 0 :(得分:6)
你的for循环中有一个无限循环!
for (var i = 1; i < arguments.length; i+2)
i+2
永远不会增加i。您需要执行i+=2