开发者

Why is this code crashing my browser?

The following code suppose to take a starting location and create an array of other locations around it and it's distance to them. I tried to debug, but the crash happens at the very beginning of this code execution (on latest chrome and 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);
}

I call it using makeRoads("a"/*the origin*/,"b",3/*first destination and distance*/,"c",4)


You have an infinite loop in your for-loop!

for (var i = 1; i < arguments.length; i+2)

i+2 never increments i. You need to do i+=2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜