Bug with counting coins in "bookkeeper" level (using javascript)


#1

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 :slight_smile:


#2

Can you take a screenshot where It’s located on the map?
(never-mind I found it.) I’ll help you in a min.


#3

Just in case and for future reference, on the top right of the pyramid on the bottom right of the desert world

it's here!


#4

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;
}
}

// Tell Naria how many enemies you defeated.

hero.moveXY(59, 33);
hero.say(defeated);


#5

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?


#6

I think, when you’re hero killed your enemies he collected some coins, and your hero didn’t count them up.


#7

Oh yeah, and if you wanna join the clan here https://codecombat.com/clans/572e74fffc0cb1a80176a206


#8

I ckecked but no, he started collected the coins afterwards, as he coins doesn’t appear until you give the count of enemys killed


#9

before you start try hero.gold = 0; or the variable you’re using.


#10

@RenziYT
I reallly appreciate that you’re trying to help, but you should really read my code first :wink: and if you don’t know either, well, let’s wait until someone does :yum:


#11

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…


#12

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.


#13

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…


#14

It worked for me this is my code.

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.

#15

Ow, but you’re using the “cheatty way” by just picking up one coin :yum:


#16

Still it works.
(20)


#17

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 :expressionless: but this method is boring


#18

Please do not revive dead threads. This one has been left alone for 2 months. :slightly_smiling_face:


#19

what if we need to revive it bc we need help to a similar problem in it


#20

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.