The Two Flower {JAVASCRIPT} Help

this is my code

// If the peasant is damaged, the flowers will shrink!

function summonSoldiers() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

// Define the function: commandSoldiers
function commandSoldiers() {
    
}
// Define the function: pickUpNearestCoin
function PickuNearstCoin() {
    
}
var peasant = hero.findByType("peasant")[0];

while(true) {
    summonSoldiers();
    commandSoldiers();
    pickUpNearestCoin();
    
}

What do you need help with? Do you have an error?

u have to define functions …

That’s correct. The contents of the function should be pretty self-explanatory, though.

P.S. function PickuNearstCoin() should be function pickUpNearestCoin().

Don’t @ everyone, it’s unnecessary

help have erorr >> hero.command(friend, “attack”, enemy);

// If the peasant is damaged, the flowers will shrink!

function summonSoldiers() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

// Define the function: commandSoldiers
function commandSoldiers() {
    var friend = hero.findFriends();
    
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        hero.command(friend, "attack", enemy);
    }
}
// Define the function: pickUpNearestCoin
function pickuNearstCoin() {
    var item = hero.findNearestItem();
    if (item) {
        hero.move(item.pos);
    }
}
var peasant = hero.findByType("peasant")[0];

while(true) {
    summonSoldiers();
    commandSoldiers();
    pickuNearstCoin();
    
}

In your code, friend is an array. You have:

function commandSoldiers() {
    var friend = hero.findFriends();
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        hero.command(friend, "attack", enemy);
    }
}

Instead, try something like:

function commandSoldiers() {
    for (let friend of hero.findFriends()) {
        let enemy = friend.findNearest(hero.findEnemies());
        if (enemy) hero.command(friend, "attack", enemy);
    }
}
1 Like

line 2
ERORR

Delete the first character on the line and replace it with a space (not the i, the thing that looks like a space)

Wait no, it’s saying there is an error on line 15, you didn’t put a starting bracket for the if (enemy)

3 Likes

i already try to replace it did’nt work

where to put if(enemy)? at top?

nvm i complete the lvl

Mark the solution please

2 Likes

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