Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introducing JavaScript Finishing the Game Wrapping It Up

Engin Tandogan
Engin Tandogan
615 Points

Small bug found in the code

Original code: //note: winningScore is set to 100 previously

function itemHandler(player, item) { ...... ...... if (currentScore === winningScore) { createBadge(); } }

//currentScore === winningScore test may never catch the createBadge() function because the score doesn't have to increment linearly due to stars and coins. If the upcoming next frame's score goes to 105 from currently 85 at once, createBadge() will never be executed forever. Correction to the code is: currentScore >= winningScore

3 Answers

Engin Tandogan
Engin Tandogan
615 Points

Although I believe the whole post is clear enough to get my point across, thanks for the input, Steven.

Steven Parker
Steven Parker
231,269 Points

I'll assume we can agree there is opportunity for improvement here, but it's more than one small bug and it would involve more than a one-liner to do it properly.

Steven Parker
Steven Parker
231,269 Points

But if you make the test "currentScore >= winningScore", wouldn't it award the badge over and over for every item acquired from then on?

Engin Tandogan
Engin Tandogan
615 Points

The code in there doesn't have a game terminating mechanism. It is experimental and for learning. So, in all cases game never ends actually. We pretend that the game ends if we make it to 100. It is like we agree to that and accept it as if the game has ended there. So, that being said, the condition to ending of game is the score 100. And to test that condition is actually buggy because star and coin and poisin are different items rated at different values. Imagine, if your current score is 80, if you grab one more star, it will get you jump up to 105. There, you have just overflown the game end condition. Again, the real mechanism to end the game is not added, it is just the score and drawing the screen some messages for won or lost. My claim is based on the fact that we refer 100 as the end of game.

Steven Parker
Steven Parker
231,269 Points

When you said *createBadge() will never be executed forever", it sounded like you expected this code would be executed more than once.