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 Scope and Loops Review Scope and Loops

Matthew Sinclair
Matthew Sinclair
595 Points

What am I missing that the quiz is explicitly asking for?

The question is:

"Create a for loop that will begin with a value of 5 and end with a value of 25. In each iteration, add the incrementing value to mathTotal. When the loop has finished running, set 'isComplete' to true. (HINT: the last value used INSIDE the loop should be 25)"

In Xcode I included NSLog for both the bool and the final value. The results of the code are as follows:

2016-10-10 21:39:05.331284 Test[1929:72178] The problem is complete. 2016-10-10 21:39:05.332439 Test[1929:72178] 25

This quiz still gives me a "Bummer! Better check your syntax!" error. If there was a syntax error, Xcode would yell at me and tell me, however this isn't the case. The only alert I got was that mathTotal might not be initialized (since I left it null), so I simply changed it to 0 - however I get the error regardless on the website regardless of mathTotal's time of initialization.

variable_assignment.mm
    int mathTotal = 0;
    bool isComplete;

    for(int i = 5; i <= 25; i++){

        mathTotal = i;

        if(mathTotal == 25){
            isComplete = true;
        }
        else{

            isComplete = false;
        }
    }
Matthew Sinclair
Matthew Sinclair
595 Points

I've even adjusted the code that runs in Xcode (have tried this as well):

int mathTotal;
bool isComplete;
int goalMathTotal = 25;

for(int i = 5; i < 26; i++){

    mathTotal = i;

    if(mathTotal == goalMathTotal){
        isComplete = true;
        NSLog(@"The problem is complete.");
    }
    else{

        isComplete = false;
    }
}

NSLog(@"%i", mathTotal);

and the site still says there is a syntax error. I'm really confused, both pass 25 through the loop, and when the loop is complete set the value of isComplete to true :(

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hey Matthew,

A bit simpler than you might think, just a few things:

  • In each loop 'increment' the amount
  • at the end of the loop set 'isComplete' to true
int mathTotal = 0;
bool isComplete;
for (int i=5; i<=25; i++) {
  mathTotal += i;  
}
isComplete = true;
Matthew Sinclair
Matthew Sinclair
595 Points

Hahaha totally overthinking it lol.

Thanks ;)

Damien Watson
Damien Watson
27,419 Points

Hehe, yeh it's usually the simpler solution. :)