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 Control the Flow with Conditional Statements

Thomas Mai
Thomas Mai
385 Points

Why 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();
  }
}
Manish Giri
Manish Giri
16,266 Points

It would be nice if you could format your code. As it stands, it's hard to read.

4 Answers

Yuanjian Liao
Yuanjian Liao
451 Points

Hi, 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
Thomas Mai
385 Points

That was exactly it. Thanks!

Thomas Mai
Thomas Mai
385 Points

Sorry, 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
Manish Giri
16,266 Points

You 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
Thomas Mai
385 Points

Thank 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?

Hello. 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.