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

[SOLVED] I need help on this (Cloudrip Commander)

This level is driving me crazy and I have been working on it for hours. Here’s my code.

// Summon some soldiers, then direct them to your base.

// Each soldier costs 20 gold.
while (hero.gold > hero.costOf(“soldier”)) {
hero.summon(“soldier”);
}

var soldiers = hero.findFriends();
var soldierIndex = 0;
// Add a while loop to command all the soldiers.
while(soldierIndex < soldiers.length){
var soldier = soldiers[soldierIndex];
hero.command(soldiers, “move”, {x: 50, y: 40});
soldiers += 1;
}
// Go join your comrades!
hero.moveXY(54,40);

I would most appreciate some help, thanks!

// Summon some soldiers, then direct them to your base.

// Each soldier costs 20 gold.
while (hero.gold > hero.costOf('soldier')) {
hero.summon('soldier');
}

var soldiers = hero.findFriends();
var soldierIndex = 0;
// Add a while loop to command all the soldiers.
while(soldierIndex < soldiers.length){
var soldier = soldiers[soldierIndex];
hero.command(soldiers, 'move', {x: 50, y: 40}); # it is {"x": 50, "y": 40}, 
# it is soldier you're commanding, not SOLDIERS
soldiers += 1; # use soldierIndex += 1 instead
}
// Go join your comrades!
hero.moveXY(54,40);

for loop is much easier

for (soldier in soldiers){
    hero.command(soldier, 'move' {'x': 50, 'y': 40})

Hi JavascriptHero,
Welcome to Discourse!

Keep going with your code, it’s nearly there. Two things:

hero.command(soldiers, “move”, {x: 50, y: 40});

The hero can only command 1 soldier at a time, so change ‘soldiers’.

soldiers += 1;

Think about which variable you’re trying to increase (again, it isn’t ‘soldiers’).

As EpicCoder says, you can probably make things shorter with a for loop - but there’s nothing wrong with nailing the while loop first :slightly_smiling_face:.

thanks, but i already figured it out