Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Sarah Newton2,179 Points
Why 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"
The 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
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.