Mountains - Slalom, Help me find a Code never finishes Solution

Hey there,
I have a Problem that my Code never finishes.
Here is my Code:

var gems = hero.findItems();
var xPlusTenX = 20;
var xPlusTenGem = 25;
var nextGem = 0;
while(true) {
    while (hero.pos.x < xPlusTenX && hero.pos.y < 35) {
        hero.move({'x': xPlusTenX, 'y': 35});
        if(hero.pos.x === xPlusTenX && hero.pos.y === 35){
            break;
            }
    }
    
    while (hero.pos.x <= xPlusTenGem && hero.pos.y >= 25) {
        var gemsIndex = gems[nextGem];
        //hero.say(gemsIndex);
        if(gemIndex){
            hero.move(gemsIndex.pos);
            if(hero.pos.x === xPlusTenGem){
                xPlusTenX = xPlusTenX + 10;
                xPlusTenGem = xPlusTenGem + 10;
                nextGem++;
                break;
            }
        }
    }
}

I know what the Problem is but I’m lacking in finding a solution.
Im finished the Level, but the Problem is that after picking up the last Gem, the hero tries to find the next one.
Though there are no more Gems he hangs up in a loop.

I tried it with an if tho find out if nextGem === 3 but then he stoped at the first base right when he walks.

I would be happy about all tips for a solution.

1 Like

@Tobias_Kreuzig Interesting solution. How did you implement the above logic statement?

[edit]
I also tried running your code and it wouldn’t run for me. Check your variable names, there looks to be a issue with one of them. Fixing that, the code does run, and logically “would work” right? But it doesn’t complete the level.

Again look at the logic statement that I highlighted and ask yourself what value will be stored in nextGem “after” you collect the last gem. Use paper and pencil if you have to, so that you can keep track of the values as you trace through the loop.

2 Likes

Thx for the Answer.
I will try that and give you my solution :wink:

1 Like