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 trialThomas Mai
385 PointsWhy wont this else if code work to add "poison" and "star"? It seems the same on the video, but it isn't running.
// when the player collects an item on the screen
function itemHandler(player, item) {
item.kill();
currentScore = currentScore + 10;
} else if (item.key === 'poison') {
currentScore = currentScore - 25;
} else if (item.key === 'star') {
currentScore = currentScore + 25;
}
if (currentScore === winningScore) {
createBadge();
}
}
4 Answers
Yuanjian Liao
451 PointsHi, I think you mess up the brackets. There should be an even number of them in the code. Apparently, you have nine of them. Second of all, you should have an if statement before all those else-if statements. Hope this helps.
Thomas Mai
385 PointsThat was exactly it. Thanks!
Thomas Mai
385 PointsSorry, I am very new to coding. I am not sure how to format it in a post. It looks like the same JS format when I post the question though. How do I make it less condensed?
Manish Giri
16,266 PointsYou can post your code within three backticks. Backtick is the key (generally) above your tab key - `
There is also a "markdown cheatsheet" beneath the textbox where you type your posts, you can refer that too.
Code Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.
```html <p>This is code!</p> ```
Thomas Mai
385 PointsThank you. It has been updated. I moved on in the course, but I still never got the code to work. Could one extra space make it not run? I am wondering if it was a browser issue?
Christian Fanell
669 PointsHello. I donΒ΄t know what your code is supposed to do but our brackets is a mess. Try this:
function itemHandler(player, item) { item.kill(); currentScore = currentScore + 10;
if (currentScore === winningScore) { createBadge(); } else if (item.key === 'poison') { currentScore = currentScore - 25; } else if (item.key === 'star') { currentScore = currentScore + 25; } }
That should work.
Manish Giri
16,266 PointsManish Giri
16,266 PointsIt would be nice if you could format your code. As it stands, it's hard to read.