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.

iOS Swift Collections and Control Flow Control Flow With Loops Working With Loops

Richard Barquero
Richard Barquero
474 Points

explanation of the question and the answer in the challenge

I've done some research and found this to be the first part of the answer. I just am confused on two items:

1) what am I being asked to get the sum of? the number of numbers in the numbers constant or the am i looking to get the value of thier sum?

while counter < numbers.count { sum += numbers[counter] counter += 1 }

2) why does counter get placed in square brackets as if i am creating an array with it.

3) also the .count is a method correct? a method which just counts the number of items there are?

let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below

1 Answer

Magnus Hållberg
Magnus Hållberg
17,232 Points
  1. Like the challenge explanation say, you are supposed to calculate the sum of the values in the numbers array.
  2. Counter gets placed inside square braces because you use that to access the index for the values in the array. It starts out as 0 (zero) and gets updated by 1 every iteration of the while loop (counter += 1).
  3. Correct, it counts the elements of an array and returns an integer.

Hope this helps