How to stop infinite loop in Java script?

i keep gettin infinite loop in level stranded in the dunes

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    }
    else {
        hero.moveXY(position.pos.x, position.pos.y);
    }
}

function atk(enemy) {
    if (enemy && enemy.type != "sand-yak") {
        if (hero.canCast("chain-lightning", enemy)) {
            hero.cast("chain-lightning", enemy);
        }
        else if (hero.distanceTo(enemy) < 10) {
            moveTo(enemy);
        }
        else if (hero.isReady("bash")){
            hero.bash(enemy);
        }
        else {
            hero.attack(enemy);
            hero.shield();
            
        }
    }
}
hero.moveXY(120,39);
while(hero.time < 100) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        atk(enemy);
    }
    else {
        hero.moveXY(120, 39);
    }
}

while(true) {
    var enemy = hero.findNearestEnemy();
    var item = hero.findNearestItem();
    if (item) {
        moveTo(item);
    }
    else if(enemy) {
        atk(enemy);
    }
    else {
        hero.move({'x':60, 'y':35});
    }
}

what pass in java script

I think unfortunately codecombat does not like while (something) loops, and it can often cause “infinite” loops. Instead try while true, with if hero.time < 100 holding all of your code, and then else break. (from the loop)

1 Like

codecombat really doesn’t like while (whatever) loops. Many will get “infinite loop” because they run for too long. Try to avoid using them, and like deadpool said, use while True because codecombat expects it to run for a long time.

how u type break in javascript

in python they use “pass” to avoid infinite loop then how u do that in javascript?