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

iOS Objective-C Basics Practicing with Immersive Examples Immersive Examples Code Challenge

Arjun Parashar
PLUS
Arjun Parashar
Courses Plus Student 2,150 Points

I'm getting super confused with this code challenge. Please help.

Please help me with this code challenge. I have watched the video twice but still I am getting confused. Please help.

variable_assignment.mm
bool levelFinished;
int rubies = 0;
int rubyReward = 7;
int rubyMax = 50;
while (levelFinished) {
  rubyReward = arc4random()%2;

1 Answer

Ryan Lee
Ryan Lee
13,806 Points

Hey Arjun,

So one problem is that you are using the levelFinished bool as your condition to check when to end the while loop. However, this bool has no value set so you're probably getting some kind of nil error I guess?

The challenge says that the condition we want to check is whether or not our variable "rubies" has exceeded the "maxRubies" variable, and if not, inside the while loop we want to increment rubies by rubyReward, so we're essentially just adding 7 to rubies as long as rubies isn't already >= 50.

Lastly, once the while loop finishes it's process, it will exit and when it exits - we know that rubies is now 50 or greater. So now we can set the bool to true for levelFinished

bool levelFinished;
int rubies = 0;
int rubyReward = 7;
int rubyMax = 50;

while (rubies < rubyMax) {
  rubies += rubyReward;
}

levelFinished = true;