[Solved]Grim Determination Javascript help

// Your goal is to protect Reynaldo
// Find the paladin with the lowest health.
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = hero.findFriends();
    for (var f = 0; f < friends.length; f++) {
        var friend = friends[f];
        if (friend.type != "paladin") {
            continue;
        }
        if (friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}
function commandPaladin(paladin) {
    var enemy = paladin.findNearestEnemy();
    // Heal the paladin with the lowest health using lowestHealthPaladin()
    // You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    // Paladins can also shield: command(paladin, "shield")
    // And don't forget, they can attack, too!
    var needsHealing = lowestHealthPaladin();
    if (needsHealing) {
        hero.command(paladin, "cast", "heal", needsHealing);
    } else {
        hero.command(paladin, "shield");
        hero.command(paladin, "attack", enemy);
    }
}
function commandGriffin(griffin) {
    var enemy = griffin.findNearestEnemy();
    hero.command(griffin, "attack", enemy);
}
function commandPeasant(peasant) {
    var item = peasant.findNearestItem();
    hero.command(peasant, "move", item.pos);
}
function commandFriends() {
    // Command your friends.
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        if (friend.type == "peasant") {
            commandPeasant(friend);
        } else if (friend.type == "griffin-rider") {
            commandGriffin(friend);
        } else if (friend.type == "paladin") {
            commandPaladin(friend);
        }
    }
}
while (true) {
    commandFriends();
    // Summon griffin riders!
    if (hero.gold >= hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
    }
}

There are no errors but the level fails. What can I do to fix this?
Thanks

You should use ===, not ==. == is for Python only.

I did that but it still fails.

// Your goal is to protect Reynaldo
// Find the paladin with the lowest health.
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = hero.findFriends();
    for (var f = 0; f < friends.length; f++) {
        var friend = friends[f];
        if (friend.type != "paladin") {
            continue;
        }
        if (friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}
function commandPaladin(paladin) {
    var enemy = paladin.findNearestEnemy();
    // Heal the paladin with the lowest health using lowestHealthPaladin()
    // You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    // Paladins can also shield: command(paladin, "shield")
    // And don't forget, they can attack, too!
    var needsHealing = lowestHealthPaladin();
    if (needsHealing) {
        hero.command(paladin, "cast", "heal", needsHealing);
    } else {
        hero.command(paladin, "shield");
        hero.command(paladin, "attack", enemy);
    }
}
function commandGriffin(griffin) {
    var enemy = griffin.findNearestEnemy();
    hero.command(griffin, "attack", enemy);
}
function commandPeasant(peasant) {
    var item = peasant.findNearestItem();
    hero.command(peasant, "move", item.pos);
}
function commandFriends() {
    // Command your friends.
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        if (friend.type === "peasant") {
            commandPeasant(friend);
        } else if (friend.type === "griffin-rider") {
            commandGriffin(friend);
        } else if (friend.type === "paladin") {
            commandPaladin(friend);
        }
    }
}
while (true) {
    commandFriends();
    // Summon griffin riders!
    if (hero.gold >= hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
    }
}

New code.

That’s only one error.

1 Like

Are you sure the paladin can cast heal when there is a lowest halth paladin?

You’ve got to say if(paladin.canCast(“heal”)){
hero.command(paladin, “cast”, “heal”, needsHealing);
}

I add the if statement, are there any other errors?

// Your goal is to protect Reynaldo
// Find the paladin with the lowest health.
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = hero.findFriends();
    for (var f = 0; f < friends.length; f++) {
        var friend = friends[f];
        if (friend.type != "paladin") {
            continue;
        }
        if (friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}
function commandPaladin(paladin) {
    var enemy = paladin.findNearestEnemy();
    // Heal the paladin with the lowest health using lowestHealthPaladin()
    // You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    // Paladins can also shield: command(paladin, "shield")
    // And don't forget, they can attack, too!
    var needsHealing = lowestHealthPaladin();
    if (needsHealing) {
        if (paladin.canCast("heal", needsHealing)) {
            hero.command(paladin, "cast", "heal", needsHealing);
        }
    } else {
        hero.command(paladin, "shield");
        hero.command(paladin, "attack", enemy);
    }
}

function commandGriffin(griffin) {
    var enemy = griffin.findNearestEnemy();
    hero.command(griffin, "attack", enemy);
}
function commandPeasant(peasant) {
    var item = peasant.findNearestItem();
    hero.command(peasant, "move", item.pos);
}
function commandFriends() {
    // Command your friends.
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        if (friend.type === "peasant") {
            commandPeasant(friend);
        } else if (friend.type === "griffin-rider") {
            commandGriffin(friend);
        } else if (friend.type === "paladin") {
            commandPaladin(friend);
        }
    }
}
while (true) {
    commandFriends();
    // Summon griffin riders!
    if (hero.gold >= hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
    }
}

new code

You’ve got to say in commandGriffin:

if (enemy){
  hero.command(griffin, "attack", enemy) 
}

or else if there’s no enemies they’re going to attack null and it launches an error.

1 Like

Also, I don’t think there is always time for the paladin to shield before attacking. Maybe you could say when the paladin’s health is lower than a certain number they retreat and shield. I’ve seen other players do that.

1 Like

I have done both of those. The level still fails however.

I can see no problem, does it launch an error?

No error, but level fail.

I guess it depends on your technique, then.

What do you mean? Should the hero attack?

This is wrong:

else {
        hero.command(paladin, "shield");
        hero.command(paladin, "attack", enemy);
    }

It was explained many times almost in every topic about Grim Determination.

function commandPaladin(paladin) {
    var enemy = paladin.findNearestEnemy();
    // Heal the paladin with the lowest health using lowestHealthPaladin()
    // You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    // Paladins can also shield: command(paladin, "shield")
    // And don't forget, they can attack, too!
    var needsHealing = lowestHealthPaladin();
    if (needsHealing) {
        if (paladin.canCast("heal", needsHealing)) {
            hero.command(paladin, "cast", "heal", needsHealing);
        }
    } else if (paladin.health < 20) {
        hero.command(paladin, "shield");
    } else {
        hero.command(paladin, "attack", enemy);
    }
}

This is my paladin function. Is there in issue in it?

I think it’s Ok now - did you test the program?

I think he did test it.

I tested it and there is one problem: I no one paladin needs healing all paladins stop fighting. So same code a little bit modified:

function commandPaladin(paladin) {
    var enemy = paladin.findNearestEnemy();
    var needsHealing = lowestHealthPaladin();
    if(needsHealing && paladin.canCast("heal", needsHealing)) {
        hero.command(needsHealing, "shield");
        hero.command(paladin, "cast", "heal", needsHealing);
    } else if(enemy) {
     hero.command(paladin, "attack", enemy);   
    }
}
1 Like

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