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 trialJames Pask
Courses Plus Student 839 PointsGiven an array of numbers, print out each number in the array using a while loop and the println statement. How?
I tried multiple different lines of code but cant get it to work
James Pask
Courses Plus Student 839 Pointslet numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var index = 1
while index = number {
println(numbers[index])
index++
6 Answers
Logan R
22,989 PointsYou are using the while loop wrong. The rest is fine. You want to loop to run as long as the index is smaller than the total number of numbers in the array.
Count how many numbers are in the array by using .count
(IE: todo.count).
(Use < instead of =)
James Pask
Courses Plus Student 839 PointsThank You for the help :)
Logan R
22,989 PointsNo problem! If you still can't get it to work, come back and post your code here and I will see if I can help you more :)
James Pask
Courses Plus Student 839 Pointsok got a new problem :(
I thunk i added what you said and the error that came up was that I needed to use println() but I did so think I just got this all muddled up :/
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers.count
var index = 1
while index < numbers.count {
println(numbers[index])
index++ }
Logan R
22,989 PointsYou need to start your index at 0. When you count numbers, most people count: 1, 2, 3, 4...etc but most programming languages, including Swift, start counting at 0: 0, 1, 2, 3...etc.
James Pask
Courses Plus Student 839 PointsThankyou. That did the trick :)
McKenna Rowe
2,930 PointsLike Zak, I'm wondering why you had to add numbers.count after the array? That didn't come up during the lesson. Is that "setting the count" in a sense?
Maher Alhasan
974 Pointslet numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] numbers.count var index = 0 while index < numbers.count { println(numbers[index]) index++ }
Gaston Trussi
528 Pointsnumbers.count is not necessary in the second line.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var index = 0
while index < numbers.count {
println(numbers[index])
index++
}
Zak Lewis
6,514 PointsLogan is right, it should look like this:
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] numbers.count var index = 0 while index < numbers.count { println(numbers[index]) index++ }
Jason Hernandez Berland
1,575 PointsZak Lewis why did you add
numbers.count
after the array?
also shouldn't index be a constant instead of a variable since the numbers array is a constant?
tomkosina
16,232 Pointslet numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] numbers.count var index = 0 while index < numbers.count { println(numbers[index]) index++ }
Logan R
22,989 PointsLogan R
22,989 PointsCan you please post your code?
Try to use proper mark-up. Put three ` before and after your code.