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

Please advise on revisions to display winning & losing text on coin game

Originally I was able to display the winning message text if someone won my coin game. However, when I tried to add losing message text neither appeared. Please help! Thank you!

3 Answers

Steven Parker
Steven Parker
231,153 Points

The code has unbalanced braces — the "else" on line 176 is missing an open brace, and the closing brace on line 185 doesn't seem to have a matching open brace.

Also, a plain "else" doesn't use a condition expression in parentheses. Plus no "lose" variable has been created yet.

And line 177 references "lossingMessage", but nothing by that name has been defined yet, either. But you don't need one, both messages could use "winningMessage".

Finally, the "update" function is perhaps not the best place to do either of these things since it gets called over and over as the game runs. A better place to display a winning message might be after line 98 where "won" is set, and to display a losing message after line 85 when the game is determined to be over.

Steven Parker
Steven Parker
231,153 Points

You might want to get familiar with the developer tools in the browser, they are very helpful in finding issues like these!

The second snapshot has these issues:

  • the declaration of "livesText" is missing
  • the "platforms.setAll" call on line 55 is missing the third argment
  • the creation of "livesText" (in the "create" function) is also missing
  • the braces are unbalanced again, the "update" function has no closing brace

Why is my code not running? https://w.trhou.se/17xyy7x7ig

https://w.trhou.se/u2aoxijq77

Please see my comments on this and advise. Thank you

Steven Parker
Steven Parker
231,153 Points

I've added some comments to my answer, but these are different issues.
Anything further should probably go in a new question.

Thank you, the developer tool is not giving me the same error messages as you are and is continuously confusing me. Every time I make corrections using developer tools for this game, I seem to get more errors that are not relevant. for example, developer tools is telling me there is a problem with the function "render" closing brace not the "update" function closing brace. Also, when I highlight the closing braces for "update" they are showing balance. I'm not sure why it's showing imbalance for you and I'm not sure why developer tools is telling me there is an error in function "render".

Steven Parker
Steven Parker
231,153 Points

A missing brace breaks the organization of the code, so the tools make a guess at where the issue is. The main thing is that once you know a brace is missing, you can look for that issue specifically.

When checking braces with highlighting, look at both ends for one that might be part of a different block. Being very consistent about indenting to show nesting levels can also help with finding this kind of issue.