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.

C# C# Objects Encapsulation and Arrays Ternary If

What is wrong with this: int value = -1; string textColor = null; textColor = (value < 0 ) ? "red" : "green";

I keep getting the error Did you include the condition?

int value = -1; string textColor = null; textColor = (value < 0 ) ? "red" : "green";

For the last line used the following reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator

CodeChallenge.cs
int value = -1;
string textColor = null;
textColor = (value < 0 ) ? "red" : "green";

//if (value < 0)
//{
//    textColor = "red";
//}
//else
//{
//    textColor = "green";
//}

2 Answers

Steven Parker
Steven Parker
215,372 Points

Your solution is technically correct, and would work fine in a practical application. :+1:

However, the challenge validation system seems to be confused by the extra space inside the parentheses. So just write it this way to pass the challenge:

textColor = (value < 0) ? "red" : "green";

You could also omit the parentheses entirely.

Steven, thank you for the time you have taken to look at this. (And apologies for the duplicate question! I have not used this type of discussion before and my mistakes were immediately apparent as I progressedwith the question...) After posting the question and logging out I realised that the challenge validation system probably didn't like the commented outcode. The next time I logged in, I deleted the comments and it worked fine. I can't remember whether I also deleted the extra space! I shall keep this in mind in future.

Steven Parker
Steven Parker
215,372 Points

Mary Hough — Without the space it passes even with the comments left in.

Glad I could help. You can mark a question solved by choosing a "best answer".
And happy coding!