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 trialNathan Robite
3,314 PointsIt's giving me an "Invalid left-hand side in assignment" error at my first variable
I wrote my code and by testing it and using F12 I was able to clear up most of my mistakes. However, now it is saying "Invalid left-hand side in assignment" and it is pointing at my first "var" for my first variable. I'm puzzled what this could mean and how to fix it.
Nathan Robite
3,314 Points''' var bear = prompt('What kind of bear is best?'); if ( bear.ToUpperCase() = "Black Bear" ) { window.alert('Fact: Bears eat beets!'); score += 1; } else { alert('False. Black bear.'); } ''' That's the first batch of code. It pops up the prompt asking the question, but stops running after the response is given and in the console it gives the error.
2 Answers
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHI Nathan,
Please use valid markdown. That means using backticks instead of quotes. There is a Markdown Cheatsheet linked at the bottom of every question field that you can use to refer to, and a preview button on the bottom right of every question field so that you can check that your post will actually look like you expect.
As for your code, you have a couple of problems on this line:
if ( bear.ToUpperCase() = "Black Bear" ) {
Two syntax errors and one conceptual error.
One syntax error is, like Antti Lylander pointed out, that you are using =
which means assignment when you mean to use ==
or ===
for comparison.
The other syntax error is that the string method for converting to all uppercase is toUpperCase()
NOT ToUpperCase()
.
Your conceptual error is that your if statement will never ever evaluate to true. If someone provides "Black Bear" as an answer, using toUpperCase()
will change that to "BLACK BEAR". Since "BLACK BEAR" is not equal to "Black Bear", no user input will ever evaluate to "Black Bear" and so the user will be forever deprived the opportunity to discover the black bear's diet.
Hope that clears everything up for you,
Cheers
Alex
Nathan Robite
3,314 PointsThank you! It took me awhile to realize what you were hinting at with the conceptual error. I'm glad you did it that way instead of just telling me cause it helped me think and then realize where I had gone wrong! I got the program up and running as planned.
Nathan
Alex Koumparos
Python Development Techdegree Student 36,887 PointsGlad to hear it! Happy coding!
Antti Lylander
9,686 PointsAntti Lylander
9,686 PointsAlways share your code when asking questions, please. It will be then easy to help you. :)
Since your course is about conditional statements, I'd guess you are using = for comparison. It does not work, you need to use == or === for comparisons.