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 Final Code Challenge

ariana palafox
ariana palafox
1,445 Points

Confused at the conflicting instructions

"The player's life goes up when the player touches a bottle of poison. Change the game so the player's life count decreases by one when a bottle of poison is touched."

That doesn't make sense to me. The life goes up, and then it goes down when the poison is touched? I thought maybe it meant to say, make the life decrease by 1 every time the poison is touched which is what I did in the code but then I get this message: "Bummer: Collecting poison does not decrease the life" But it does.

What am I missing?

3 Answers

Bella Bradbury
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Bella Bradbury
Front End Web Development Techdegree Graduate 32,790 Points

Hi!

So the challenge is stating that currently the player gains a life when they touch the poison but it needs to actually take a life away. So in the existing challenge code it adds a life and we need to change it so it takes away a life.

If you attempted to take away a player life and then got the 'bummer...' message, it means that there's something else wrong with your code. The 'bummer...' message is supposed to be a hint that the code you've currently changed does not take away a life yet.

I can see how that could be a little confusing! From here it's hard to help without seeing your code, if you're still stuck be sure to post your code. To give you a hint: only one character needs to be changed in the existing challenge code to pass the second step!

ariana palafox
ariana palafox
1,445 Points

Hello! Thanks for the reply. I guess what else I’m confused about is that the code i put in did what was asked. See code:

function poisonCollect(player, poison) { poison.kill(); if(poison.key === 'poison'){ lives = lives - 1; } if (lives === 0) { player.kill(); gameOver = true; } }

I figured out that what it wanted was simply the lives = lives - 1 part but is there a reason why my little code did not pass since it did succeed in getting the result?

Thanks!

Bella Bradbury
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Bella Bradbury
Front End Web Development Techdegree Graduate 32,790 Points
(poison.key === 'poison')

doesn't work because a key property is never set at all poison variables. It's essentially looking for a nested property titled key and then checking to see if i's value matches 'poison'. Since the property as a whole doesn't exists in the first place, that conditional statement will always evaluate to false and the code will never run.

Something that would have worked is:

function poisonCollect(player, poison) { 
  poison.kill(); 
  if(poison){ 
    lives = lives - 1; 
  } 
  if (lives === 0) { 
    player.kill(); 
    gameOver = true; 
  } 
}

In this scenario, you're checking to see if the player touched poison and then removing a life if they did! This would not be DRY code as the poisonCollect() function already only works if it is poison, but it would still be valid code.

I hope this helps :) Sometimes I wish the challenges had a console so you could debug. It can be so hard on the challenges to figure out the issue!

ariana palafox
ariana palafox
1,445 Points

Thanks Bella,

Thing is, the code DID work when run. The little man scooted along, got the poison and lost one life each time he touched a poison. So now I’m even more confused 😭 since you said its not supposed to work.

Maybe down the line it’ll make more sense, I guess. Thanks for your help.