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 trialShaun Jones
1,062 PointsNot accepting the Ternary code
The error asks if I have included the boolean comparison in the statement. I have, what am I missing here?
int value = -1;
string textColor = null;
(value < 0) ? textColor = "red" : textColor = "green";
2 Answers
Steven Parker
231,248 PointsThe termary operator takes 3 operands: the condition, the true expression and the false expression. The entire thing returns a value, but it is not a complete statement in itself, no more than "count < 8
" would be.
Termaries should not be used to control execution flow. So it would not normally contain assignment statements, though it's common for it to be used in one.
A typical use of a ternary might have a structure like this:
somevariable = conditional_expression ? value_if_true : value_if_false;
Shaun Jones
1,062 PointsThe one about the boolean expression:
int value = -1;
string textColor = null;
(value < 0) ? textColor = "red" : textColor = "green";
The one with the variable in front:
int value = -1;
string textColor = (value < 0) ? textColor = "red" : textColor = "green";
Shaun Jones
1,062 PointsI just solved it. I did it by doing what you stated, taking the assignment portion out. Things didn't click for a little bit, even after you mentioned it. However, I don't understand why the first block of code wasn't being accepted.
Steven Parker
231,248 PointsThe challenge may impose additional criteria beyond just successful compilation and execution.
Shaun Jones
1,062 PointsShaun Jones
1,062 PointsI have used it nearly the exact same way as the course has (except the course puts a return prior to the start of it. I have tried it the way you are describing it (variable = condition ? true : false;) and it still errors out. The error specifically being "Did you include the "red" and "green" string literal expressions (separated by a colon) to the right of the ternary operator?".
When I don't have the variable in the front, the message says "Did you include the boolean expression to evaluate?". Which I have done so. At this point, I am not sure what they are trying to say.
Steven Parker
231,248 PointsSteven Parker
231,248 PointsThose hints seem pretty explicit. Perhaps you should show your code again now that you've taken the assignments out of the ternary. Just guessing from the description, the one with the "variable in the front" is probably the closest to being correct.