Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Nils Garland
18,416 Pointswhat shall i do?
what shall i do?
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
2 Answers

jcorum
71,813 PointsFirst you create a while loop that runs while counter is less than the length of the array:
while counter < numbers.count {
sum += numbers[counter]
counter++
}
Then you add the value of the array at each index to sum and increment counter.

jcorum
71,813 Pointssum += numbers[counter] is shorthand for sum = sum + numbers[counter].
numbers[counter] gets the value that is in the element of the numbers array that has the index value of counter. So numbers[0] gets the value at index 0, numbers[1] gets the value at index 1, etc.
When all is said and done, the variable sum will hold the sum of the values in the array, or, in 43.

jon kelson
5,149 PointsThank you for explaining that jcorum . I just get a bit confused why the tests don't always test you on exactly what you have just learnt in the videos before. I guess it's to make us think a little more and find out these things. Thanks
jon kelson
5,149 Pointsjon kelson
5,149 Pointssum += numbers[counter]...... Hi jcorum in the video were taught to put a println in the centre . could you help me please and explain the centre part of your solution. sum += numbers[counter] many thanks Jon