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 trialIzzatilla Karimov
9,284 PointsMagic number and strings...
I don't understand what I'm doing wrong, here is the question... (Replace the magic numbers and strings in the if / else if / else statement with constants.)
And here is the error ( Bummer! Did you define a constant for the magic string 'red'?)
And here is my code
const int revenue = 125000; string status = null;
const int rev1 = 100000; const int rev2 = 150000; const string red = "Red"; const string yellow = "Yellow"; const string green = "Green";
if (revenue < rev1) { status = red; } else if (revenue < rev2) { status = yellow; } else { status = green; }
1 Answer
Dale Bailey
20,269 PointsHi the problem is hard to see without proper markdown, at a glance it could be the value of the strings in your code starts with a capital letter (common trap on treehouse) Heres the working code to compare.
const int revenue = 125000;
string status = null;
const string red = "red";
const string yellow = "yellow";
const string green = "green";
const int min = 100000;
const int max = 150000;
if (revenue < min)
{
status = red;
}
else if (revenue < max)
{
status = yellow;
}
else
{
status = green;
}