Need help with medical attention level in js, expected identifier in first "else" and cant get it fixed


#1

// Ask the healer for help when you’re under one-half health.

while(true) {
var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;
// If your current health is less than the threshold,
// move to the healing point and say, “heal me”.
// Otherwise, attack. You’ll need to fight hard!
var enemy = hero.findNearestEnemy()
if (hero.health < hero.maxHealth)
hero.moveXY(65, 46);
hero.say(“heal me”);
else {
if (enemy) {
if hero.isReady(“cleave”);
hero.cleave(enemy);
}
else {
hero.attack(enemy);
else {
hero.attack(enemy);
}


#2

You can find instructions here in the FAQ on how to properly post your code so that it is formatted properly. It’s really difficult to tell what’s wrong looking at your code in its present format.


#3

I haven’t done this yet being that I am not that far yet since I’m stuck somewhere else but I formatted your code for you and I’ll give you my solution to the level. I think you have your code written with too many if’s and else’s causing an error because it’s looking for and else if statement but I am unsure but try this.

while(true) {
var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;

var enemy = hero.findNearestEnemy()
if (hero.health < hero.maxHealth) 
hero.moveXY(65, 46);
hero.say("heal me");
else {
if (enemy) {
if hero.isReady("cleave");
hero.cleave(enemy);
}
else {
hero.attack(enemy);
else {
hero.attack(enemy);
}

In your code I would of written in this way:

while(true) {

var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;
var enemy = hero.findNearestEnemy()

   if (hero.health < hero.maxHealth) {
        hero.moveXY(65, 46);
        hero.say("heal me");

} else if (enemy && hero.isReady("cleave")) {
        hero.cleave(enemy);

} else {
        hero.attack(enemy);
  }
}

I will break it down for you on why I wrote it the way I did and you can let me know if it works for you.
first you assigned the variables like so

var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;
var enemy = hero.findNearestEnemy()

then you have to make an if statement that would move your character to a certain coordinate and ask to be healed when your health is at a certain number that is less then your max health.

   if (hero.health < hero.maxHealth) {
        hero.moveXY(65, 46);
        hero.say("heal me");
}

then you had to write and else if statement that would cleave any enemies as long as cleave was ready to be used like so

} else if (enemy && hero.isReady("cleave")) {
        hero.cleave(enemy);

The reason I wrote it this way is because from what I have learned you write if statements like this: you start with a plain if statement followed by else if then by else. But you can have as many else if statements between the starting if statement and the ending else statement as you want for example:

   if (hero.health < hero.maxHealth) {
        hero.moveXY(65, 46);
        hero.say("heal me");

} else if (enemy && hero.isReady("cleave")) {
        hero.cleave(enemy);

} else if (enemyDistance > 10 && hero.health < hero.maxHealth) {
        hero.shield();

} else {
        hero.attack(enemy);
}

So pretty you start and if statement like so.

   if (hero.health < hero.maxHealth) {
        hero.moveXY(65, 46);
        hero.say("heal me");

Than anything in here can be else if statements of w.e you like:

else if statements
else if statements
else if statements
else if statements
else if statements

finally ending with just a plain old else statement

else {
        hero.attack(enemy);
}

Well let me know if my explanation helped you. I’m still learning as well but this is how I would of done your challenge and the reasoning behind it. Also for future reference you highlight your code and click the </> to put it in a code block so it is easily readable or you can type ``` before and after your code.


#4

i tried doing what you said but now my hero isnt surviving


#5

Can you be more specific what part of the code is failing?

Maybe if you try it this way with 2 different if statements.

while(true) {

var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;


if (hero.health < hero.maxHealth) {
        hero.moveXY(65, 46);
        hero.say("heal me");
    }

var enemy = hero.findNearestEnemy()

if (enemy && hero.isReady("cleave")) {
        hero.cleave(enemy);

    } else {
        hero.attack(enemy);
    }
}

The first if statement would be the one that looks too see if your health is less than your max health. Like so

if (hero.health < hero.maxHealth) {
        hero.moveXY(65, 46);
        hero.say("heal me");
    }

Then we can make another if statement for the attacking like so:

if (enemy && hero.isReady("cleave")) {
        hero.cleave(enemy);

    } else {
        hero.attack(enemy);
    }
}

Since the if statements are on the same indentation they will run separately I think, and since in the second if statement the else is indented therefore it will on run within that if statement.

Give that a go and tell me if it works for you.

Also I am pretty sure you can make this into 2 different functions like so:

Function 1:

var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;

function needHealing();
if (hero.health < hero.maxHealth) {
        hero.moveXY(65, 46);
        hero.say("heal me");
    }

Function 2:

var enemy = hero.findNearestEnemy()

function cleaveOrAttack(target);

if (enemy && hero.isReady("cleave")) {
        hero.cleave(enemy);

    } else {
        hero.attack(enemy);
    }
}

Then you would run your while loop like so:

while (true) {
   needHealing();
   cleaveOrAttack(enemy);
}