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 Swift Collections and Control Flow Control Flow With Loops For In Loops

I do not understand what the question is asking for

I do not understand what the task is asking for, so if someone can explain to me what it is looking for and how I should approach it that would be great, not only giving me the answer. Thanks.

loops.swift
// Enter your code below
var results: [Int] = []

for a in results 

1 Answer

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Amer Albareedi,

I'll just preface my response with ... to understand swift 'for in' loops better, take a look at The Swift programming language documentation

Just going to quote the relevant challenge steps for reference ... and then investigate it below.

Your task for this first step is to create a for in loop that iterates over a range. Multiplication tables typically range from 1 to 10 (with 10 included) so the range you are iterating over should go from 1 to 10. Note: Leave the body of the loop empty for this task.

For in loops also define a constant that temporarily stores the value in the iteration process. For the loop you're writing, name this constant multiplier.

// Enter your code below
var results: [Int] = []

Ok, so in this step we don't need to worry about the multiplier (6) but we do need to consider the range 1 to 10, and the iterator which they've requested to be names 'multiplier', which for the for in loop. The structure will look like ..

for iterator in range_to_iterate_over {
    // code here
};

The iterator will take on each value of the range increasing by 1 each cycle/iteration through the loop. So if the range is 1 to 10 (1...10) then the first time through it will have a value of 1, second time through it will have a value of 2, and so forth up until it reaches the end of the range, with it's final value being 10 in our example.

So now let's replace that template with requested challenge data and we get.

for multiplier in 1...10 {
};

The challenge specifically said to leave the body empty for task 1, but in task 2 it is in the body where you will perform the calculations and append the result in each iteration to the results[] array. If you get stuck I can absolutely help you there .. but I think you'll get much more from the challenge if you have a read of the documentation I linked above, or re-watch the series video covering this and work out the solution :smile:

Best of luck, and happy coding :dizzy:
Dave

Thank you very much Dave