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 trialSarah Newton
2,179 PointsWhy is this ternary operator not valid?
I believe this is written correctly yet the challenge fails to recognize the boolean operator. Am I missing something obvious here?
int value = -1;
string textColor = null;
(value < 0)
? textColor = "red"
: textColor = "green"
1 Answer
andren
28,558 PointsThe ternary operator is valid, but the way you use it is not. The ternary operator returns a value based on the expression you give it. So instead of placing an assignment in the true
and false
section you should just type the value that should be assigned. And then assign the ternary operator to the textColor
like this:
textColor = (value < 0) ? "red" : "green";
You had also forgotten to end the ternary operator with a semicolon. And while it is allowed to break the operator onto separate lines it is not usually done unless the values are long enough to warrant it. Since part of the point of the ternary operator is that it is pretty space efficient.