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 trialoyemayowa sanwo
861 PointsIs there a way I can see the answer to this question so I can figure out my error.
I do not get the while and do loop. Do I always have to create an index? I also don't get the println(todo[index]) index++. He did not explain to a novice what exactly that expression means. Its really depressing not being able to move on from a very easy module
2 Answers
Steve Hyun
1,798 PointsI just checked for you. Your code is right, you just have to organize better. For some reason the way the editor in the lesson reads the code is wrong. You just have to organize it better. Try to make it look like this and it should work.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var index = 0
while index < numbers.count {
println(numbers[index])
index++ }
Steve Hyun
1,798 PointsI know its frustrating. I am a novice myself and have been frustrated hundreds of times on this journey. Hopefully I can help.
You don't always have to specifically create an index. He's just created the variable index as a number to reference at which point of the array you're on.
The index is initialized to 0. todo.count is the total number of entries in the array which is 5.
What the while loop is saying is this: While 0 is less than 5, print the number of the variable index (which is now set at 0) of the todo array. So since arrays start at 0, we would print "Return calls" to the console. The "index++" means simply to add 1 to the index.
This doesn't end here. While loops go on forever until there is a condition that stops it.
So since we've added one with index++, the index's value is now 1. So the loop goes back to the top and continues. Now it says:
While 1 < 5, print todo[1], which is the second entry in the array: "Write blog" Now we add 1 again to the index so the index value is now 2.
This continues until finally the index is equal to 5.
At this point, 5 is not less than 5 so the while loop stops here.
Hope this helps!
oyemayowa sanwo
861 PointsI am still struggling. I am not sure what the forum rules are but I did this let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] var index = 0
while index < numbers.count {println(numbers[index]) index++ }. It keeps saying its a consequtive statement that needs to be separated. If I attempt to separate with ;, I get no result at all in the assistant editor. I also wondered if I needed to specify numbers as an integer that I need to print or my var index will suffice to inform the system that I actually need the integers from the array to print. Can you tell me the flaw in my reasoning so I can figure out how to fix it.
oyemayowa sanwo
861 Pointsoyemayowa sanwo
861 PointsThank you so much. It did work but only after I did exactly what you said. I really appreciate your help.