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

Anna Benson
Anna Benson
2,728 Points

Having trouble iterating a for loop correctly within an array

I am stuck on this code challenge: "Inside the body of the loop, we're going to use the multiplier to get the multiple of 6. For example, if the multiplier is 1, then the multiple is 1 times 6, which is equal to 6. 
Once you have a value, append it to the results array. This way once the for loop has iterated over the entire range, the array will contain the first 10 multiples of 6."

I tried out my code on the playground, and when I ran it and took a look at the Debug area, it appeared that my code printed the first ten multiples of 6.

However, I got an error code saying my results array does not contain the first 10 multiples of 6.

Is there something I'm missing?

loops.swift
// Enter your code below
var results: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for multiplier in 1...10 {
    print(multiplier * 6)
}

3 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Anna Benson,

You don't need any pre-populated values in the results array. Your for loop will populate the empty array. You have your loop set up properly, however, you're not executing the right statement inside of it. The code you want to execute in each iteration of the loop is results.append(multiplier * 6). This code will add the values of multiplier times 6 to the results array for each iteration. See my code below:

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

for multiplier in 1...10 {
    results.append(multiplier * 6)
}

Hope this helps! Good Luck!

Anna Benson
Anna Benson
2,728 Points

Thank you for clarifying!

Steven,

Thank you for the answer! Although, appending to results and iterating a for in loop with an empty array already declared was not covered in the preceding video. That is most likely the culprit for us having to go through hoops to find the answer. Again, thanks. ***Other than this issue, it's a fantastic and informative course!