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 While and Do-While Loop

while loop quiz Swift

I need help for the do-while / while loop quiz in Swift basics

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var index = 1
while index == 1 {
      println(numbers)
      index++
}

1 Answer

Hi Lucas,

I am not going to give you a straightforward answer, but will let you do that by yourself and I will just focus you in the right direction :)

The best way to solve this problem is to analyze your code as if you were analyzing a sentence you wish to say to someone. In this case:

You wanted to say:

I want to count all the numbers from 1 to 10

so,

My first index count is 1 and while that index count is less than my numbers total count I wish to:
- say the number on that index out loud  (println)
- add +1 to index ~ (++) so that I know I counted that number of my numbers
each time

but you made two small mistakes and said

while my index count is equal to 1 I wish to:
- say all of my numbers out loud (println)
- add +1 to index ~ (++) so that I know I counted that number of my numbers
each time.

did you notice the difference?

while that index count is less than my numbers total count

and

while my index count is equal to 1

also

say the number on that index out loud (println)

and

say all of my numbers out loud (println)

Hope this will put you into a right direction.

Ask again if you got confused or something like that

Best regards, Alex

I don't get it, When I put in:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var index = 1
while index == 1 {
      println(numbers)
      index++
}

it prints [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]] instead of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Ok. First you are checking if the index is 1, you need to check if the index < numbers.count.

Second, you need to printout each number, one by one, not all of them at once. numbers[index]

Third, try setting the index first to 0.