\\ Only works when I insert the hero.say("x"). If I remove it it says slow or infinite loop.
function fightAndReport(seconds) {
var defeated = 0;
while (hero.time <= seconds) {
var enemy = hero.findNearestEnemy();
hero.say("Going to fight")
if (enemy) {
hero.attack(enemy);
if (enemy.health <= 0) {
defeated += 1;
}
}
}
hero.moveXY(59, 33);
hero.say(defeated);
}
function collectAndReport(seconds) {
while (hero.time <= seconds) {
var item = hero.findNearestItem();
hero.moveXY(item.pos.x, item.pos.y);
}
hero.say(hero.gold);
}
fightAndReport(15);
collectAndReport(30);
fightAndReport(45);
The < in that line be > instead
No, it’s correct
replace the condition with a true
and put if (hero.time > seconds) break;
at the start of the loop
Use ++
instead: defeated++;
Using let
instead of var
is better
Remove hero.say
unless it’s required