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

Swift 2.0

Challenge Task 2 of 2

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.

Bummer no work i need help please ....

loops.swift
// Enter your code below
var results: [Int] = []
for i in 1...10 {
let multiplier = 6 * 1
results.append(multiplier)
}
for multiplier in 1...10 {
results.append(multiplier * 6)
}

2 Answers

Hi Ary,

The challenge asks us to calculate the 6 times table by using a for-in loop.

I see you have two separate for-in loops in your code above where you have tried to do this. The first one is incorrect, the second one should be correct. To solve the challenge, you can simply remove the first for-in loop from your code and it should work.

Here's a detailed explanation if you need it:

First, the challenge already provides us with an array of type Int, like so:

var results: [Int] = []

Next, let's look at your first for-in loop, shown below:

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

This loop will iterate through the numbers 1 through 10. The first time through the loop, 'i' is equal to 1. The second time through the loop, 'i' is equal to 2. This continues until the last iteration of the loop where 'i' = 10. It's a pretty good start, but if you look at the statements inside your loop, you aren't using 'i' at all. Here is the statement portion of your first loop:

  let multiplier = 6 * 1
  results.append(multiplier)

What's happening here is that you are declaring a constant called 'multiplier' and assigning it a value of 6 * 1, which always equals 6. Then you are appending this constant to the array.

If I listed out the calculation of your first for-in loop, it would look something like this:

  • 1st loop: 'i' = 1, 'multiplier' = 6, append 'multiplier' to 'results'
  • 2nd loop: 'i' = 2, 'multiplier' = 6, append 'multiplier' to 'results'
  • 3rd loop: 'i' = 3, 'multiplier' = 6, append 'multiplier' to 'results'

...this continues until...

  • 10th loop: 'i' = 10, 'multiplier' = 6, append 'multiplier' to 'results'

As you can probably now see, this will result in an array full of 6's because each time through the loop, you are declaring a constant called 'multiplier' and assigning it a value of 6!

If it helps, try using my explanation of how a for-in loop works and walk through your second for-in loop to see if it makes sense now. Here is the second for-in loop you wrote:

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

Hope this helps!

Aqui esta a resposta correta para o problema.

var results: [Int] = [] for multiplier in 1...10 { var multiplicationOfSix = multiplier * 6 results.append(multiplicationOfSix) }