Why does function cleaveWhenClose(enemy) return errors when function cleaveWhenClose(target) works well?

This below formula works:

function cleaveWhenClose(target) {
if(hero.distanceTo(target) < 5) {
if (hero.isReady(“cleave”))
hero.cleave(target);
// else, just attack target!
} else {
hero.attack(target);
}
}
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
cleaveWhenClose(enemy);
}
}

but I tried to keep things simple by changing parameter name to enemy like the codes below. It doesn’t work. Why not?

function cleaveWhenClose(enemy) {
if(hero.distanceTo(enemy) < 5) {
if (hero.isReady(“enemy”))
hero.cleave(enemy);
// else, just attack target!
} else {
hero.attack(enemy);
}
}
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
cleaveWhenClose(enemy);
}
}

I’m not sure that is a actual command…

if (hero.isReady(“enemy”))
hero.cleave(enemy);
// else, just attack target!
} else {

You missed a bracket

Not sure if I understand what you mean, do I NEED to keep the
"function cleaveWhenClose(target) {"
for it to be a proper command?

Why can’t I change the variable (target) into the variable (enemy)?
“function cleaveWhenClose(enemy) {”

Ok, never mind about that

@kasakuri You can. JavaScript is about variable and dynamic coding. There are a few problems with your code I believe. The first is in my previous comment, you missed a bracket. Also I noticed in your second one, you wrote:

if (hero.isReady("enemy"))

Shouldn’t it be:

if (hero.isReady("cleave"))
1 Like