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

for in loop

I was asked to create a loop of numbers from 1 to 10, multiply the numbers by 6 in the body of the loop and then append the result in an array so that the array will contain the the results for the first ten multiples of 6. So I did this but it's obvious my code is not correct. Please help.

loops.swift
// Enter your code below
for multiplier in 1...10 {
    let b = 6 * multiplier
    var array = []
    array += [b] 
}

1 Answer

Hi. You do not need to create a constant to multiply the range by 6. Here is what i did:

var results: [Int] = []

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

Also the array should've already been created for you, so there is no need to make another one. It is much easier and more efficient to do the above, rather than what you did

 let b = 6 * multiplier

and then appending it to an array inside the loop.

Anyway, I hope that was clear. Good Luck :)

Thank you so much.