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 trial

C#

Steve Agusta
Steve Agusta
6,221 Points

C# Objects Code Challenge: Magic Numbers and Constants

This one seems like it should be easy, but the last few code challenges have not really made sense to me. It's not clear what the objective is with this one, and I'm only getting an error asking "did you delete the revenue variable?" Am I supposed to delete the revenue variable? I have tried deleting the revenue variable. I have tried not deleting the revenue variable. I get the same error message either way. The feedback on this code challenge does not make sense. Also, the challenge itself seems odd, why would you go to the trouble of writing the constants when the if/else statements seem fine as it is?

int revenue = 0; string status = null; const int revenue1 = 100000; const int revenue2 = 150000; const string status1 = "red"; const string status2 = "yellow"; const string status3 = "green";

if (revenue < revenue1) { status = status1; } else if (revenue < revenue2) { status = status2; } else { status = status3; }

1 Answer

andren
andren
28,558 Points

I agree that this exercise is to put it mildly pretty bad, arguably one of the worst I have seen on treehouse.

Anyway, as it seems you have gathered from the code you have written. The objective of the challenge is to replace all strings and number with variables. The challenge is indeed not explained very well. And while excessive use of magic numbers and strings is something that is not ideal, having a certain number of them is perfectly normal. Converting every single number and string to a variable is a contrived task that would never be done in a real application.

But I digress, your solution is almost entirely correct. But the revenue declaration seems to have gotten messed up at some point while you were trying to figure out what to do. At the start of the challenge the revenue variable is declared like this:

const int revenue = 125000;

And it's one of the variables that you should not touch at all. Besides that your code is correct. So if you change the revenue variable like so:

const int revenue = 125000;
string status = null;
const int revenue1 = 100000;
const int revenue2 = 150000;
const string status1 = "red";
const string status2 = "yellow";
const string status3 = "green";

if (revenue < revenue1) {
    status = status1; 
} else if (revenue < revenue2) {
    status = status2; 
} else {
    status = status3; 
}

Then your code will pass.

Steve Agusta
Steve Agusta
6,221 Points

Thanks! I actually worked it out just after posting this. Bizarre, this challenge.