Level multiplayer treasure grove with js

this is mi code:

loop {
var enemy = this.findNearestEnemy();
var coins = this.findNearestItem();
var distance = this.distanceTo(enemy);
if (coins){
var pos = coins.pos;
var x = pos.x;
var y = pos.y;
this.moveXY(x, y);
}
else if (distance < 10){
this.isReady(“cleave”);
this.isReady(“cleave”);
}
}
I have an error on line 4 that says: "distanceTo target is null"
help me please, don’t know what it could be

Hint: Format your code with 3 ` back ticks so people can try the code.
The code works fine to me, but is cleaving neccesary as I saw you didn’t attack with cleave you checked if it was ready but never striking him or anything.

Hint: are you sure that an enemy exists, before calculating the distance?