Help with Prime Pathing

I need help with the level prime pathing in the glacier. This is my code:

function primePos(mines){
    for(var i = 0; i < mines.length; i++){
        var mineValue = mines[i].value;
        for(var n = 2; n < mineValue - 1; n++){
            if (mineValue % n !== 0) {
                return mines[i].pos;
            }
        }
    }
}
function findMinesInRange(mines, range){
    var minesInRange = [];
    for(var i = 0; i < mines.length; i++){
        if (hero.distanceTo(mines[i]) <= range) {
            minesInRange.push(mines[i]);
        }
    }
    return minesInRange;
}

var section = 1;
while (true) {
    var mines = findMinesInRange(hero.findHazards(), 10);
    var Pos = primePos(mines);
    var friends = hero.findFriends();
    hero.moveXY(Pos.x, Pos.y);
    for(var i = 0; i < friends.length; i++){
        hero.command(friends[i], "move", Pos);
    }
}


And this is what happens:


She walks into a mine and dies.
I don’t know what is going wrong, and I need help! :sob:

1 Like

Your code to find primes is almost there, but there’s a few things wrong with it:

If the number isn’t divisible by any number it will be returned (390 isn’t divisible by 20, but it is by 2)
So if it IS divisible you can rule it out as a prime.
However if it goes through every number from 2 to mineValue/2 (mineValue -1 works too but mineValue/2 will need to run fewer times.) without being divisible by n, then it is prime and should be returned.
I recommend setting a variable to true before the inside for loop and then setting it to false if the number passes mineValue % n === 0 and only returning it after the inside loop if it is still true.

TLDR; yout function is currently finding if they are not prime instead of if they are prime.

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.