Siege of Stonehold help [JavaScript]

I have the following code, but it says that there is an unmatched bracket somewhere. It also will not run.

// Help your friends beat the minions that Thoktar sends against you.
// You'll need great equipment and strategy to win.
// Flags might help, but it's up to you–be creative!
// There is a doctor behind the fence. Move to the X to get healed!

loop {
    var enemy = this.findNearest(this.findEnemies());
    var flag = this.findFlag();
    var hp = this.health;
    var distance = this.distanceTo(enemy);
    
    if (flag){
        this.pickUpFlag(flag);
    } else if (enemy){
        if (distance < 15){
            this.attack(enemy);
            if (this.isReady("cleave")){
                this.cleave(enemy);
            } else {
                this.shield();
            }
        }
    if (hp < 150){
        this.moveXY(19, 69);
    }
}

SOLVED BY:

loop {
var enemy = this.findNearest(this.findEnemies());
var flag = this.findFlag();
var hp = this.health;

if (flag){
    this.pickUpFlag(flag);
} else if (enemy){
    var distance = this.distanceTo(enemy);
    if (distance < 15){
        this.attack(enemy);
        if (this.isReady("cleave")){
            this.cleave(enemy);
        } else {
            this.shield();
        }
    }
if (hp < 150){
    this.moveXY(19, 69);
}

}
}

Hello, NinjaBlox, and welcome. Please read the FAQ before you post again, to learn how to format your code properly. I have done it for you this time, but do so yourself the future.

You didn’t close the else if on line 14. That is your problem.

[redacted, we don’t post correct code]
This should do it propperly,also try to improve your indenting because for now your code is not that easy to understand in a instant.