Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Power Points (JS) Nesting Issue?

Code seems to basically work; I think my problem is a nesting issue. Hero moves to the first point, casts spell VENI (Latin command for ‘come here’ which I actually use with my children!), and then he kills the skeleton.

And then that’s it. He just sits there. It doesn’t continue to loop through and move on. Thanks for the help.

// Loop through the powerMap to find positive numbers.
// First, loop through indexes of rows.
for (var i = 0; i < powerMap.length; i++) {
    // Each row is an array. Iterate through it.
    for (var j = 0; j < powerMap[i].length; j++) {
        // Get the value of the i row and j column.
        var pointValue = powerMap[i][j];
        // If it's a positive number:
        if (pointValue > 0) {
            // Use convert to get XY coordinates.
            var pos = convert(i, j);
            // Move there, say "VENI" and be prepared!
            hero.moveXY(pos.x, pos.y);
            hero.say(spell);
            while (true) {
                var enemy = hero.findNearestEnemy();
                if (enemy && enemy.health > 0) {
                    hero.attack(enemy);
                }
            }
        }
    }
}

For the record, I did add the Javascript line to the triple back ticks for the code, but I guess I did it wrong…will try to search today on that and get it fixed.

Hi, the problem is that you’re using a while(true) loop. When do you tell the loop to stop looping?
Once you’ve changed the loop there will be another bit you’ll need to change, just a warning.
-Danny
P.S.
I think it might because you’ve got a capital J. It works for me like this:
```javascript
```

2 Likes