Bomemender level - can't seem tot attack "all enemies" until i change te base (level) code

As a Novice, it bothered me that my code was accepted but i didn’t see my mage attack all the enemies

Original code

while(true) {
    if (hero.canCast("regen")) {
        var bernardDistance = hero.distanceTo("Bernard");
        if(bernardDistance < 10) {
            // "Bernard" needs regeneration!
            hero.cast("regen", "Bernard");
        }
    }
         if (hero.canCast("regen")) {
        var chandraDistance = hero.distanceTo("Chandra");
        if(chandraDistance < 10) {
            // "Bernard" needs regeneration!
            hero.cast("regen", "Chandra");
        // Use "if" and "distanceTo" to regenerate "Chandra"
        // if she is closer than 10 meters away.
        
        }
    }
    else {
        // If you aren't casting "regen", use "if" and "distanceTo"
        // to attack enemies that are closer than hero.attackRange.
        var enemy =  hero.findNearestEnemy();
        if (enemy){
        if (hero.distanceTo(enemy) < hero.attackRange){
            hero.attack(enemy);
            
        }
    }

}
} 

it seemed the code got stuck on the hero.canCast(“regen”) and only attacked after bernard was healed. and got stuck again on chandra.

it seemed to help to put the distance before the isReady
But as this is not how the level was mend to be fixed, i as a novice want to know if the masters think.

Changed code (not as intended, I suspect)

while(true) {
               var enemy = hero.findNearestEnemy();

 
        if(hero.distanceTo("Bernard") < 10) {
               if (hero.canCast("regen")) {
            hero.cast("regen", "Bernard");
        }
        }
        if (hero.distanceTo("Chandra") < 10){
               if (hero.canCast("regen")) {
        hero.cast("regen", "Chandra");
}
}
        

    else {
        if (enemy){
            if (hero.distanceTo(enemy) <hero.attackRange)
           hero.attack(enemy);
        }

        
        
    }
}

it is my first topic so i Hope i the the Backtick thing right to keep it readable( it keeps commenting on it when i want to post)

thanks in advance