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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Brice Corbin](https://secure.gravatar.com/avatar/7d727b1c691cff434e6f6ccc2246c10a?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Brice Corbin
1,872 PointsCan't get through this, spent an hour on it. Really feels instructions are unclear... help please
Can't work it out
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < numbers.count {
counter++
}
4 Answers
![Jennifer Nordell](https://uploads.teamtreehouse.com/production/profile-photos/5432702/micro_jen-square.jpg)
Jennifer Nordell
Treehouse TeacherWhat it's asking for is for you to loop through the array that they set up at the top and figure out what all those numbers added together are. They set you up a variable named sum so that you can keep track of what the current total is. So through the first time through the loop the sum will be 2. Second time the sum will be 10... 2 + 8, The third time sum will be 11... 10 + 1. Fourth time, 27 ... 11 + 16. Take a look at the small snippet you need.
while counter < numbers.count {
sum += numbers[counter]
counter++
}
Here while our counter is less than the number of elements in the array we take sum and then add the number that's at the position of counter to our current running total aka sum. Hope this helps!
![Jason Anders](https://uploads.teamtreehouse.com/production/profile-photos/3512822/micro_Jason_s-Pic.jpg)
Jason Anders
Treehouse Moderator 145,859 PointsHey Brice,
This one requires a bit of logic in the thinking process, but also have to make sure not to overthink it.
The second part of the challenge wants you to use the loop to add all the numbers in the array up. So far your code is on the right track, except you are missing the line that will calculate the sum.
We will use the counter
variable to access the specific values in the array by their indexes. If this isn't familiar to you, I'd recommend going back and reviewing the video introducing Arrays.
The instructions for Task 2 gives an example of the equation you will need to use. This line of code needs to go before the counter gets incremented, so the correct index is being accessed.
Below is the corrected code for the task. I hope it makes sense once you see it. :)
while counter < numbers.count {
sum = sum + numbers[counter]
counter++
}
![Brice Corbin](https://secure.gravatar.com/avatar/7d727b1c691cff434e6f6ccc2246c10a?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Brice Corbin
1,872 PointsThanks jennifer, I actually did this at the very beginning but had my counter++ before and was retrieving an error so moved away from it and spent an hour looking for other solution. really frustrating... I actually managed to do it another way but seams ur code makes way more sense than mine....
while counter < numbers.count { var newValue = numbers[counter] counter++ sum += newValue }
![Cindy Lea](https://secure.gravatar.com/avatar/8d36cc5aed706061021be7cbd92078c9?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Cindy Lea
Courses Plus Student 6,497 PointsI dont see instructions. I think you need to add a statement in your while loop to add the numbers up that are in the numbers array.
sum += sum[count]
You want to add the numbers in the numbers array using indexing. Start with sum[0] all the way up to sum[6}
![Cindy Lea](https://secure.gravatar.com/avatar/8d36cc5aed706061021be7cbd92078c9?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Cindy Lea
Courses Plus Student 6,497 PointsIm sorry. Its should be sum += numbers[counter]
Jason Anders
Treehouse Moderator 145,859 PointsJason Anders
Treehouse Moderator 145,859 PointsJennifer types faster than I do.![:smiley: :smiley:](https://image-proxy-cdn.teamtreehouse.com/f266e5f075421fb8b3a44f1b8d72534e51ae5854/68747470733a2f2f7374617469632e7465616d74726565686f7573652e636f6d2f696d616765732f656d6f6a692f756e69636f64652f31663630332e706e67)
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherJason Anders I have to if I'm ever going to get above 30k points :)
Steven Deutsch
21,046 PointsSteven Deutsch
21,046 Points30k... I'm just trying to break 20k :D
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherSteven Deutsch Luckily, you won't be trying for very long! You're almost there :)