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

boris said
boris said
3,607 Points

I am not having errors, but the question won't accept my result. Can someone please help me?

This is my code. I got to step 2. Your help is appreciated in advanced

var results: [Int] = []

for multiplier in 1...10 {

print("\(multiplier) times 6 is equal to \(multiplier * 6)")
print(multiplier)


results = [multiplier * 6, multiplier * 6, multiplier * 6, multiplier * 6, multiplier * 6, multiplier * 6,multiplier * 6, multiplier * 6, multiplier * 6]

}

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

for multiplier in 1...10 {

    print("\(multiplier) times 6 is equal to \(multiplier * 6)")
    print(multiplier)


    results = [multiplier * 6, multiplier * 6, multiplier * 6, multiplier * 6, multiplier * 6, multiplier * 6,multiplier * 6, multiplier * 6, multiplier * 6]
}

2 Answers

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

You don't need to print anything, so you can get rid of the two print lines in your code.

I also think you're confused on how to add each multiple of 6 to the results. The way your code is structured, it completely changes what results is equal to upon each iteration. The first iteration, multiplier is set to 1, so results is set to [6, 6, 6, 6, 6, 6, 6, 6, 6]. The second iteration, multiplier is set to two, so results is set to [12, 12, 12, 12, 12, 12, 12, 12, 12]. This continues until multiplier reaches 10, at which point results is set to [60, 60, 60, 60, 60, 60, 60, 60, 60].

This is clearly not what the challenge wants you to do. Instead, it would be better to append results with multiplier * 6 in the for in loop:

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

I hope this helps!

Angel Caro
Angel Caro
11,831 Points

You need to append the multiplier to the results variable like so:

var results: [Int] = []

for multiplier in 1...10 {

  results.append(multiplier * 6)

}