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

what am i messing up

while numbers < numbers,count { println(numbers[index]) index ++ }

while_loops.swift
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
while numbers < todo.count {
println(todo[numbers])
numbers ++
}

You need to set a new variable to count the component of the array you're printing like an Int i. You use 'numbers' and that's the array itself. you have to increment this variable i within the loop. The loop will repeat until this variable achieves the same value as the number of elements in the array 'numbers'. I don't know why u use the variable todo, cause you didn't declare it in your code, you need to get the count property of the numbers array. Like in the println you need to get the position i in the numbers array. I give you my solution:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var i :Int = 0
while i<numbers.count {
println(numbers[i])
i++
}

1 Answer

Here is what you have wrong : You have to change the โ€œtodo.countโ€ to โ€œnumbers.countโ€

// remember that an array has index, 
// numbers[0] <-- index 0 is the number 1 in your array

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

var index = 0 // create a variable with an initial value of 0

//the loop will work until is FALSE

while index < numbers.count { // while the var INDEX is less than the total of elements in your array then do the following
    println(numbers[index]) // print the first number of the array (numbers[0])
    index++ // now increment the variable INDEX by one and start again
}

this is what it looks like without the comments

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

var index = 0 
while index < numbers.count { 
    println(numbers[index])
    index++ 
}