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 trialMichael Cafin
iOS Development Techdegree Student 1,221 PointsDo not understand the question
Hi, I am not sure what they are asking. I am not seeking for the answer but perhaps a little hint would help.
Thanks!
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while sum < numbers.count {
print(numbers[sum])
sum++
}
4 Answers
Lucas Smith
11,616 PointsHi there!
For the first Task in the challenge, they are wanting you to make a while loop that runs while the counter variable is less than the number of items in the numbers array. This is pretty close to what you have here, and it wouldn't take much to have the loop working with the correct variable.
For the second Task, they are looking for all of the values from numbers to be added together, and to use counter to facilitate this while the loop is running. This is where the sum variable comes in, as they want the final sum stored there.
I hope this makes sense! If not, or if you have other questions, just let me know and I'll be more than happy to do what I can :)
Michael Cafin
iOS Development Techdegree Student 1,221 PointsQuick reply - Thx Lucas, it is much appreciated. Currently I'm working the late shift, but I will look at this first thing tomorrow morning and I will let you know.
Thanks!
Michael Cafin
iOS Development Techdegree Student 1,221 PointsThank you Lucas! I got the first task correct by substituting the sum for the counter variable.
However, for task 2, I am not sure what the syntax should look like; I did take a look at the example given, but the word "newValue" throws me off. Is the example close to what the example should look like?
Thank you once more Lucas
Lucas Smith
11,616 PointsHi Michael, glad that I could help with the first task! Let's see what we can do about the second!
Without giving the answer explicitly, I'll explain the concept, using which iteration the loop is on to reference an array index, as best I can in code.
The first bit is that we now have a number that will grow with each iteration of the loop, but won't give us an index out of bounds error, since we stop the loop if counter is the same as the number of elements in the array. This is a rather common convention with loops, and it's quite useful! For this example, we're using it to call up the index of an array, so the general syntax of this would look like the following:
let anArray = [1,1,2,3,5,8,13,21] // Just a dummy array of Fibonacci numbers
var counter = 0 // Our good friend counter
var sum = 0 // The sum where the final value will go
while counter < anArray.count { // Makes sure the loop will stop before an
// index error rears its ugly head
sum += anArray[counter] // Adds value of the current index of the array,
// to the current value of sum
counter++ // Increment counter so that it will grow
// and the loop won't go on forever
}
I hope this all makes sense, the newValue is just a placeholder used for the example. Loops are literally everywhere, so manipulating them is something that you will get used to with time. Again, just let me know if you have any questions, and happy coding!
Michael Cafin
iOS Development Techdegree Student 1,221 PointsThank you Lucas! Your examples and comments given are helpful - especially the comments regarding "sum += anArray[counter]"
So far, I understand that we use a while loop with the Treehouse example "sum += newValue" embedded into the body. This is what I entered on the coding machine ...
while counter < numbers.count { sum += numbers[counter] counter++ }
And this is the answer I got back from the coding machine - Make sure your condition allows for iteration over all elements in the numbers array and check your calculation of sum!
Not sure what is incorrect?
Lucas Smith
11,616 PointsHmm, that really is odd that the challenge isn't liking your code. From what I can see, the syntax looks correct. Would you mind posting all of your code here? To make it show up as a black code block like the one above just put three (the lower case tilde. it should look like
``) on a line before your code, and on a line after the code. There is a link that says "Markdown Cheatsheet" right below the textbox that will show you the syntax to post it here properly.
This will just allow me to look at everything that you currently have. I look forward to this getting sorted for you!
Michael Cafin
iOS Development Techdegree Student 1,221 PointsHi Lucas! I found the mistake, it was at my end (duh!). I had the while loop of my previous task still in there, which caused the error :)
I want to thank you for all your help so far it has been helpful!
Michael
Lucas Smith
11,616 PointsI'm glad that you got it sorted out! You're very welcome! I enjoy helping people out, especially when I see "I'm not looking for the answer"! Tells me that you actually want to learn, and not just have it handed to you :)
I hope you enjoy Swift and iOS as much as I am, and Happy Coding!
Michael Cafin
iOS Development Techdegree Student 1,221 PointsHi Lucas, sorry for my late reply. I enjoy Swift and cannot wait to start my first project. I do agree with you that I rather help a person that wants to learn than one who wants to receive solely an answer from me - I personally do not learn from that either if I cannot explain ....
Jonathan Hector
5,225 PointsJonathan Hector
5,225 Pointsyou are missing ( ) in the while loop. Putting (sum <numbers.count) might help.