Java scripts good code who didn't really work

Hi for the level “A Fine Mint” i made a code for this level.

function pickUpCoin() {
var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}

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

hero.attack(enemy);

pickUpCoin();

}

but the hero take to much time for pick the coin and the level fail and say “Ran out of time”

I don’t know what to do.

If u can help me =) and sorry for my english i’m a french =^-^=

Thanks :*

@Persephone hi I see u play on java script can you help me for my problems?

Please surround your code in 3 backticks (`) above and below your code, so it will format correctly.

@Serg

‘’'function pickUpCoin() {
var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}

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

hero.attack(enemy);

pickUpCoin();

}’’’

like that?

Use ( ` ) instead of ( ’ ).

Oh sorry

var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}

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

hero.attack(enemy);

pickUpCoin();
}```

Almost, gotta use 3 of them above and below.

function pickUpCoin() {
var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}

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

hero.attack(enemy);

pickUpCoin();
}

that’s what I do, but they disapear

@Serg I just want to advance through the levels.
my code works but is not optimal and I have two coins to pick up at the end but ran out of time… So i don’t know what to do…

Hi, when I copy paste your code into the level, it doesn’t work. Are you sure you are copy-pasting correctly?

Sure I’am…

On my code in the part of the "while(true)
for the “hero.attack(enemy);” there is a “play” button on the left and and a blue and white frame around the line of code.
Can I give you a screenshot or something like this?

yes, you can drag a picture into the typing box:


like this

@Tom_H @Serg sorry for loosing your time I retry and now it’s working why? i don’t know but the champion don’t make the same decison O.o

Sorry… :confused:

1 Like

I was having a problem with opening the full chest, but it is fixed now :grin:

1 Like