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 trialFarbod Salman
1,061 PointsI cannot figure this question out!
Now that we have the while loop set up, it's time to compute the sum! Using the value of counter as an index value, retrieve each value from the array and add it to the value of sum.
For example: sum = sum + newValue. Or you could use the compound addition operator sum += newValue where newValue is the value retrieved from the array.
***Any help and explanations would be much appreciated. Thanks!
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < numbers.count{
counter++
}
while(counter < numbers.count){
sum += numbers[counter]
counter++
}
2 Answers
jcorum
71,830 PointsIt looks like your only problem is that you have two loops, rather than one, and the first is incomplete. I suggest you just delete it. As this works fine:
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while (counter < numbers.count) {
sum += numbers[counter]
counter++
}
It's interesting that the challenge won't accept counter += 1, although Swift is moving away from ++ in the next version.
Farbod Salman
1,061 PointsOh yes thank you! Seemed like a dumb mistake. I love treehouse but some of their code challenges are not well put together at all. It tells you that you need to keep all the code you have written from task one in every task but in this one you need to delete the first one that you created. Very confusing. Thanks!