If that doesn’t work, then try replacing this line:
var enemy = enemies[enemies.length - 1];
With this:
var enemy = enemies[0];
And then check if there is an enemy first. Also, I would like to point out that because you have chosen to use an array that has bsem defined before the loop, you will only be attacking enemies in that array and it won’t update. Try putting:
var enemy = hero.findNearestEnemy()
At the beginning of the while true loop (inside it), check if there is an enemy, and then attack it. You may then use whatever abilities you may have.