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

collection and flow

This code below won't. Challenge Task 1 of 2

For this challenge, we're going to use a for in loop to compute the multiplication table for 6 and append the results to an array.

To start off, I've created an empty array of type Int and assigned it to a variable named results. Note that we have to explicitly declare the type here so that the compiler knows this array will hold Int values.

Your task for this 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.

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. var results: [1,2,3,4,5,6,7,8,9,10] for task in results { print ("(task) x 6 = (task*6)")} let multiplier = results

Steven Deutsch
Steven Deutsch
21,046 Points

Hey G Deng, can you post your code? Let me see if I can help.

3 Answers

thanks, you may also refer to the question to understand what I did in the code below var results: [Int] = [1,2,3,4,5,6,7,8,9,10] for task in results { print ("(task) x 6 = (task*6)") let multiplier = for task in results { print ("(task) x 6 = (task*6)")

Steven Deutsch
Steven Deutsch
21,046 Points

If you look above the "Post" button. There's a markdown cheatsheet. Use it to format your code. It will make things more readable and easier for others to help you. Practice it by editing your response.

Steven Deutsch
Steven Deutsch
21,046 Points

Challenge Task 1 of 2:

Step 1:

Your task for this 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.

Requirement:

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] = []

// step 1: we are creating a for loop to iterate over the range 1 through 10
// requirement: the challenge requires us to name the temporary constant for the loop multiplier

for multiplier in 1...10 {

}

If you need help with the second part of the challenge, let me know. But here is a hint: Inside the body of your for loop (the body is what's inside the curly braces), you will be taking the value of multiplier and multiplying it by 6. You will then use the append() method to add the result to your results array.

please help with the second part i don't know how to append this to array.

for multiplier in 1...10 { print ("(multiplier) x 6 = (multiplier * 6)")

thanks Steve I know where the problem was now, appending inside the body. Thanks How do I edit code before posting? is there a link? thanks

brian arredondo
brian arredondo
6,535 Points

Hi I need help for the second part, I cant make it work.