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

Why can you not use the series of keywords "if, else if, else" for our item.key conditional statements?

In the video we learn how to add multiple conditional statements for the point values of the items in the game -- if, else if, else if, for 'coin', 'poison', 'star', respectively.

But lower in the game.js file, regarding applying animation to movement keys being pressed, you are allowed to use the series of conditional keywords -- if, else if, else.

When I try to do the same for item values it just breaks the code and the game doesn't load. Is the 2nd case for a different usage? Could you not apply the 2nd series of conditional keywords to the item/values used in the video?

https://teamtreehouse.com/library/control-the-flow-with-conditional-statements

Thank you.

Could you link to the video in question?

2 Answers

Hm... not quite sure what's the problem.

When you write your If()...else if()... else statement, are you accidentally including a conditional statement after the else keyword? In other words, are you following your else statement with parenthesis that are to check a condition? because if so, that may be breaking the code.

else is the code to execute if the if statement preceding it fails. That is it's condition: we do not outline an additional condition for the else keyword.

I think that sums it up. I wasn't realizing that in the 2nd block of code (pre-written), that the "else" was by itself and wasn't a conditional statement. They were all conditional statements in the first block of code.

Thank you.

Great! Glad we could figure it out.

https://teamtreehouse.com/library/control-the-flow-with-conditional-statements

Strange, I thought this question would be posted underneath the specific video lesson. I must have not posted properly, but here is the video.

Code we were creating in the video:

 // when the player collects an item on the screen
function itemHandler(player, item) {
item.kill();
if(item.key === 'coin'){
currentScore = currentScore + 10;
}
else if(item.key === 'poison'){
     currentScore = currentScore - 25;
}
else if(item.key === 'star'){
     currentScore = currentScore + 25;
}
if (currentScore === winningScore) {
  createBadge();
}

}

Code later on in the game.js file where the other keywords are used:

// is the left cursor key presssed?
if (cursors.left.isDown) {
  player.animations.play('walk', 10, true);
  player.body.velocity.x = -300;
  player.scale.x = - 1;
}
// is the right cursor key pressed?
else if (cursors.right.isDown) {
  player.animations.play('walk', 10, true);
  player.body.velocity.x = 300;
  player.scale.x = 1;
}
// player doesn't move
else {
  player.animations.stop();
}

if (jumpButton.isDown && (player.body.onFloor() || player.body.touching.down)) {
  player.body.velocity.y = -400;
}
// when the player winw the game
if (won) {
  winningMessage.text = "YOU WIN!!!";
}

}