The Agrippa Defense java script. MAJOR HELPšŸ˜

I have tried to beat this level for many days now and just canā€™t seem to do it. It says my line of code regarding ā€œvar distance = hero.distanceTo(target);ā€ is wrong. Any ideas on how to do it?
(i do not know how to format my code so sorry about that I will try to learn in the future.)

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Find the distance to the enemy with distanceTo.
        var distance = hero.distanceTo(target);
        // If the distance is less than 5 meters...
        if (distance<5) {
            
        }
            // ... if "cleave" is ready, cleave!
            if (enemy) {
                var ready = hero.isReady("cleave");
                hero.cleave(enemy);
            }
            // ... else, just attack.
            else {
                hero.attack(enemy);
            }
    }
}

Hi,
Hereā€™s how you format your code:

I did it for you this time.
The problem is in that line. What is target? Where is it defined? Have you checked if it exists?
Danny