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

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