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
170,403 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
170,403 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
170,403 Points

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