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 Basics (retired) Control Flow For-In Loop

I could not understand this challenge (for_loops.swift)

" Use a for-in loop to print out a "7 times table". Your output should look like this: 1 * 7 = 7 2 * 7 = 14 Etc., all the way up to 10. Each line should be on a new line. "


3 Answers

Chris Hedstrom
Chris Hedstrom
3,024 Points

The challenge is asking you to output 10 lines of code to the Console output (the assistant editor, which can be displayed pressing option-command-return, or View > Assistant Editor > Show Assistant Editor.

Since this a challenge using a for-in loop, remember what the for-in loop tells the comptuter to do:

for (a constant) in (an array) (do something)

like his example in the video:

for number in 1...10 {     
    println("\(number) times 2 is \(number*2)") /

this states that for a constant, "number" in the array "1...10" which is the numbers 1 through 10, print to the console "The (number) times 2 is (number multiplied by 2)". For example: "1 times 2 is 2". Because it's a for in LOOP, this will continue to execute until there are no more items in the array to perform the loop upon.

The challenge is basically the same, but the numbers and the sentence have changed. The output to the console should be something like "1 * 7 = 7". It can be completed by changing just a couple of things in the example code above.

I'm a beginner to this, so hopefully this makes sense and is helpful. If anyone sees a mistake, please clarify!

Steve Pham
Steve Pham
1,488 Points

for number in 1...10 { println("(number) times 7 table (number*7)") }

Just an input about how I cracked it. It worked for me. var number: [String] = ["1","2","3","4", "5", "6", "7","8","9'","10"] for number in 1...10{println("(number) times 7 is (number*7)")} Hope it helps. I am happy that I could do it (almost) alone. Hurray!