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

**URL:** <https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294>\
**Category:** Uncategorized\
**Created:** [January 16, 2017, 9:18pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294 "2017-01-16T21:18:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Afuldye](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/afuldye/32/5608_2.png) [@Afuldye](https://discourse.codecombat.com/u/Afuldye)\
**Post date:** [January 16, 2017, 9:18pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/1 "2017-01-16T21:18:06Z")

</div>

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:**
>
> ```python
> // 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 🙂

---

<div class="post-metadata">

**Author:** ![RenziYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/renziyt/32/5374_2.png) [@RenziYT](https://discourse.codecombat.com/u/RenziYT)\
**Post date:** [January 16, 2017, 9:47pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/2 "2017-01-16T21:47:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Afuldye](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/afuldye/32/5608_2.png) [@Afuldye](https://discourse.codecombat.com/u/Afuldye)\
**Post date:** [January 16, 2017, 9:59pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/3 "2017-01-16T21:59:03Z")

</div>

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!**
>
> ![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/2/2f4896046723597eca61576080e794665242cae3.png)

---

<div class="post-metadata">

**Author:** ![RenziYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/renziyt/32/5374_2.png) [@RenziYT](https://discourse.codecombat.com/u/RenziYT)\
**Post date:** [January 16, 2017, 10:00pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/4 "2017-01-16T22:00:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Afuldye](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/afuldye/32/5608_2.png) [@Afuldye](https://discourse.codecombat.com/u/Afuldye)\
**Post date:** [January 16, 2017, 10:03pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/5 "2017-01-16T22:03:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![RenziYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/renziyt/32/5374_2.png) [@RenziYT](https://discourse.codecombat.com/u/RenziYT)\
**Post date:** [January 16, 2017, 10:05pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/6 "2017-01-16T22:05:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RenziYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/renziyt/32/5374_2.png) [@RenziYT](https://discourse.codecombat.com/u/RenziYT)\
**Post date:** [January 16, 2017, 10:06pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/7 "2017-01-16T22:06:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Afuldye](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/afuldye/32/5608_2.png) [@Afuldye](https://discourse.codecombat.com/u/Afuldye)\
**Post date:** [January 16, 2017, 10:07pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/8 "2017-01-16T22:07:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RenziYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/renziyt/32/5374_2.png) [@RenziYT](https://discourse.codecombat.com/u/RenziYT)\
**Post date:** [January 16, 2017, 10:20pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/9 "2017-01-16T22:20:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Afuldye](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/afuldye/32/5608_2.png) [@Afuldye](https://discourse.codecombat.com/u/Afuldye)\
**Post date:** [January 17, 2017, 6:50am UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/10 "2017-01-17T06:50:35Z")

</div>

@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 😋

> [@Afuldye](#):
>
> ```python
> var defeated = 0;
> var coinCollected = 0;
> 
> ```

---

<div class="post-metadata">

**Author:** ![Afuldye](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/afuldye/32/5608_2.png) [@Afuldye](https://discourse.codecombat.com/u/Afuldye)\
**Post date:** [January 18, 2017, 2:26pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/11 "2017-01-18T14:26:13Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![RenziYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/renziyt/32/5374_2.png) [@RenziYT](https://discourse.codecombat.com/u/RenziYT)\
**Post date:** [January 18, 2017, 11:38pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/12 "2017-01-18T23:38:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Afuldye](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/afuldye/32/5608_2.png) [@Afuldye](https://discourse.codecombat.com/u/Afuldye)\
**Post date:** [January 19, 2017, 6:51am UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/13 "2017-01-19T06:51:52Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![BobBobson412](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bobbobson412/32/6871_2.png) [@BobBobson412](https://discourse.codecombat.com/u/BobBobson412)\
**Post date:** [January 19, 2017, 2:00pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/14 "2017-01-19T14:00:24Z")

</div>

It worked for me this is my code.

````python
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.
````

---

<div class="post-metadata">

**Author:** ![Afuldye](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/afuldye/32/5608_2.png) [@Afuldye](https://discourse.codecombat.com/u/Afuldye)\
**Post date:** [January 19, 2017, 2:42pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/15 "2017-01-19T14:42:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BobBobson412](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bobbobson412/32/6871_2.png) [@BobBobson412](https://discourse.codecombat.com/u/BobBobson412)\
**Post date:** [January 20, 2017, 1:48pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/16 "2017-01-20T13:48:49Z")

</div>

Still it works.  
(20)

---

<div class="post-metadata">

**Author:** ![katebot](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@katebot](https://discourse.codecombat.com/u/katebot)\
**Post date:** [April 16, 2017, 8:28pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/17 "2017-04-16T20:28:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)\
**Post date:** [April 17, 2017, 12:33am UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/18 "2017-04-17T00:33:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adam\_King](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/adam_king/32/2705_2.png) [@Adam\_King](https://discourse.codecombat.com/u/Adam_King)\
**Post date:** [November 11, 2017, 8:51pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/19 "2017-11-11T20:51:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [November 11, 2017, 10:01pm UTC](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294/20 "2017-11-11T22:01:19Z")

</div>

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.

[Next page](https://discourse.codecombat.com/t/bug-with-counting-coins-in-bookkeeper-level-using-javascript/10294.md?page=2)
