Rich forager loop problems

I have checked back and forth my code but I could not find the problem with my loop. It kept saying unreachable ‘while’ after ‘return’.

Here is my code:

loop {
    var flag = this.findFlag();
    var enemy = this.findNearestEnemy();
    var item = this.findNearestItem();

    if (flag) {
        this.cleave();// What happens when I find a flag?
        this.pickUpFlag();
        }
    
    else if (enemy) {
        this.attack(enemy);// What happens when I find an enemy?
        }
        
    else if (item){
        var pos = item.pos;
        var itemx = pos.x;
        var itemy = pos.y;
        this.moveXY(itemx, itemy);// What happens when I find an item?
        }
 }

Looks fine to me (although you’ll want to pass flag to pickUpFlag and probably move to the flag, too). Does it still give you that error if you reload the page?

If at one point you broke it with an infinite loop, it may have added “return;” to the very first line. As that line is usually a comment, it can be hard to see if you don’t check for it explicitly:

return;// Use your new skill to choose what to do: this.now()

loop {
    ...
1 Like

i have fixed the problem. Thank you