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 trialJohn Krueger
1,996 PointsBoolean expression?
Keeps saying that I need to include a boolean operator. But I thought the "?" was a bool op. what am I missing?
int value = -1;
string textColor = null;
return (value < 0) ? textColor = "red" : textColor = "green";
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi John,
You don't need the return statement because you're not trying to return a value from a method.
I believe what you have left should be passing the challenge but it's not.
(value < 0) ? textColor = "red" : textColor = "green";
The challenge might be trying to get you to follow a better practice. Notice that you're repeating the textColor variable here.
You could have the textColor on the left and then only "red" and "green" in the ternary statement.
textColor = (value < 0) ? "red" : "green";
The ternary statement will either evaluate to "red" or "green" and that result will then be assigned to textColor
This way you're avoiding repetition.
Feisty Mullah
25,849 Pointsint value = -1; string textColor = null;
return (value < 0) ? textColor = "red" : textColor = "green";
your code should look like this:
int value = -1; textColor = (value < 0) ? "red" : "green";
hope it helps.
Jason Anello
Courses Plus Student 94,610 PointsYou've taken out the declaration of the textColor
variable so it's not going to pass like that.