Need Help with Cursed Valley--Hero keeps dying

Hello everyone.

I’m currently am stuck in Cursed Valley and I think I have 2 problems. The first one is that the skeletons seem buggy. Sometimes my hero attacks them and sometimes she doesn’t. Other times, once she does kill them, they’ll like, come back to life and die again.

The second problem is that my hero always dies before the 2nd potion appears. Here’s my code:

while (true) {
    var enemy = hero.findNearestEnemy();
    // Attack if enemy exists AND enemy.team is "ogres"
    // AND enemy.type is "skeleton"
    if (enemy && enemy.team === "ogres" && enemy.type === "skeleton") {
        hero.attack(enemy);
    }
    
    var item = hero.findNearestItem();
    // Collect if item exists AND item.type is "potion"
    // AND hero.health is less than hero.maxHealth / 4
    if (item && item.type === "potion" && hero.maxHealth / 4){
        hero.moveXY(item.pos.x, item.pos.y);}
}

I don’t know if it’s a code problem or a gear problem.

Here’s my gear.

Thanks to everyone who helps me!

What is your total health right now?

My current health is 476. I only have like, 90 gems. If it’s a gear problem, whats a good way to get gems?

Thanks!

A good way to get gems is to simulate games on multiplayer levels.

if (item && item.type === "potion" && hero.maxHealth / 4)

you must compare current hero health with some value
The structure

if(codition1) action1;
if( codition2) action2;

can be replaced with

if(codition1) action1;
else if( codition2) action2;

putting the most important condition first ( the health)