Hello,
I’m new to programming and am using codecombat to learn javascript .
I have a little problem with the level bookkeeper (in the desert)
When I try this:
// Fight enemies for 15 seconds.
// Keep count whenever an enemy is defeated.
var defeated = 0;
var coinCollected = 0;
while (true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
if (enemy.health <= 0) {
defeated += 1;
}
}
if (hero.now() > 15) {
break;
}
}
// Tell Naria how many enemies you defeated.
hero.moveXY(59, 33);
hero.say(defeated);
// Collect coins until the clock reaches 30 seconds.
while(true) {
var coin = hero.findNearestItem();
if (coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
coinCollected += coin.value;
}
if (hero.now() >30 ) {
break;
}
}
// Tell Naria how much gold you collected.
hero.moveXY(59, 33);
hero.say("I have "+ coinCollected);
// Fight enemies until the clock reaches 45 seconds.
// Remember to reset the count of defeated enemies!
var defeated = 0;
while(true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
if (enemy.health <= 0) {
defeated += 1;
}
}
if (hero.now() > 45) {
break;
}
}
// Tell Naria how many enemies you defeated.
hero.moveXY(59, 33);
hero.say(defeated);
The gold count in the second part (the coinCollected variable) is always lower (from what I’ve seen so far, always one less) that the real count. I don’t understand my error.
I did manage to beat the level using hero.gold instead of a counting but still, I don’t understand what I did wrong?
Thank you to anyone willing to help me
I highlighted all the stuff you don’t need, and your mistakes below:
Bold means It’s wrong or you don’t need it
// Fight enemies for 15 seconds.
// Keep count whenever an enemy is defeated.
var defeated = 0; var coinCollected = 0;
while (true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
if (enemy.health <= 0) {
defeated += 1;
}
}
if (hero.now() > 15) {
break;
}
}
// Tell Naria how many enemies you defeated.
hero.moveXY(59, 33);
hero.say(defeated);
// Collect coins until the clock reaches 30 seconds.
while(true) {
var coin = hero.findNearestItem();
if (coin) {
hero.moveXY(coin.pos.x, coin.pos.y); coinCollected += coin.value;
}
if (hero.now() >30 ) {
break;
}
}
// Tell Naria how much gold you collected.
hero.moveXY(59, 33);
hero.say("I have "+ coinCollected); // Insted of coinCollected use hero.gold
// Fight enemies until the clock reaches 45 seconds.
// Remember to reset the count of defeated enemies!
var defeated = 0;
while(true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
if (enemy.health <= 0) {
defeated += 1;
}
}
if (hero.now() > 45) {
break;
}
}
Thank you,but as I mentioned in my first message that’s exactly how I did beat the level but it should work as well counting the coin right? Why when I’m counting i got “19” when there is actually “20” coins for example?
@RenziYT
I reallly appreciate that you’re trying to help, but you should really read my code first and if you don’t know either, well, let’s wait until someone does
I’m starting to believe it’s a bug in the level, as I saw someone having the same trouble in this level in the “feedback” topic.
As well, the question as been on for a couple of days now, and noone seems to have an answer…
It’s not a bug, sometimes your code has to be totally correct for example:
var enemy = hero.findNearestEnemy(); - if you won’t put it at the end, the code won’t work.
and I also think that instead of var coinsCollected = 0;
try to put var coinsCollected = hero.gold();
bc when you put 0 it makes your gold to zero and it just adds one all the time.
I don’t agree. The whole point is finding out why counting the coins
doesn’t work. So I don’t want to use hero.gold . It should work like it
does for the enemies
[EDIT]
And it dosn’t add only one gold at a time, as I’m using coincCollected += coin.value and the total is always quite right, as I say, I just have one gold less evrytime (like I have 19 when she expects 20) so it is counting but there is abviously a mistake somewhere…
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.now() > 15:
break
hero.moveXY(59, 33)
hero.say(defeated)
while True:
hero.moveXY(40, 45)
if hero.now() > 30:
hero.moveXY(59, 33)
hero.say("1")
break
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.now() > 45:
break
hero.moveXY(59, 33)
hero.say(defeated)
```I don't think it is a bug either because it worked for me but you could try my code and reply if it didn't work.
the problem is that you stop count coins and then you move to Naria. and when you move you still pick up coins but you don’t count them. face this problem right now but I haven’t got any solutions…for now
flags could help but I need a sword
well, using " self.gold " helped but this method is boring
The best thing to do would be create a new topic and maybe include a link to this one saying that your issue is similar. Necroposting is never a good idea.