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 trialRichard Jones
3,500 PointsDid you include the boolean expression to evaluate? Yes, I did. So now what?
Not sure what I am missing. I am looking at the code in Visual Studio and it seems to work. Any ideas?
int value = -1;
string textColor = null;
return (value < 0) ? textColor = "red" : textColor ="green";
3 Answers
Jennifer Nordell
Treehouse TeacherHi there! The problem here is in the interpretation of the instructions. They are asking you to assign the value of "red" or "green" to the variable textColor
. The instructions have not asked you to return anything.
The variable you should be assigning to goes on the left of the equals sign as normal. Then the expression to evaluate goes on the right side followed by a question mark. Finally the two values (the first for true and the second for false) go after the question mark and should be separated by a colon.
Here's an example:
myMood = happinessLevel < 0 ? "unhappy": "happy";
In this example if happinessLevel
is less than 0, then myMood
will be assigned the string "unhappy". Otherwise, it will be assigned the string "happy".
I hope this helps, but let me know if you're still stuck!
Hugh Veal
3,250 PointsYou only need to use a return statement if it is in a method. Try this code instead:
code redacted by moderator
Jennifer Nordell
Treehouse TeacherHi Hugh! Explicit answers without any explanation are strongly discouraged by Treehouse. Could you please edit your answer to include an explanation? Thanks!
Steven Parker
231,275 PointsWhen formatting code, the three marks that start and end the coding area are backticks (```), not apostrophes ('''').
Jennifer Nordell
Treehouse TeacherI have redacted the explicit answer posted above as an explanation for how/why the code works the way it does was never provided by the original poster of the answer.
Richard Jones
3,500 PointsThanks, I have been at this too long. Why was I returning anything. Taking a break.
Hugh Veal
3,250 PointsIt takes Practice, and an unbending will.