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

Daniel Schlichter
Daniel Schlichter
783 Points

No idea about the second part of the last challenge in Swift 3 Collections and Control Flow :/

I have no idea, how the second part of the last challenge in Swift 3 Collections and Control Flor works.. Can anybody help me :) ?

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

// Enter your code below
while counter < 7 { numbers 
      counter += 1
 }
while counter < numbers.count {
    (numbers[counter]) 
     counter += 1
     }

3 Answers

Hi! You are very close, but some of the syntax is incorrect, and you do not also need 2 while loops. This can also be done in one single loop --

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

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

Let's take this one line at a time.

while counter < numbers.count 

this is an alternative(and more efficient) way of doing what you did, which was

while counter < 7

If this array were to change, with values possibly being appended to it, then you would get an index out of bounds error and your program would crash. Using the .count method is a much safer way rather than hard coding it

now the next line,

sum += numbers[counter]

is done because the challenge asks to retrieve each value, and add it to the sum. You were retrieving the values, but not adding it to the sum, which can be done using the compound addition operator (+=), which is just

sum + sum = numbers[counter]

The last line:

counter += 1

was correct. By incrementing the counter, you stopped the loop from continuing forever.

I hope this helped and that you understood it all. Good luck :)

Daniel Schlichter
Daniel Schlichter
783 Points

Oh thank you so much Heath!!! :)

No problem :)

s t
s t
2,699 Points

Thanks also Heath for this answer but one thing I still don't get even though this code obviously works. Why the [counter] after sum += numbers? please explain in a bit more detail. Thanks

Adam Jones
Adam Jones
3,646 Points

In this case, counter serves as the index for the array, retrieving each item (in this case a number) so long as counter is less than the total count of items in the array. This ensures that the loop does not continue perpetually.

Was stuck on this for awhile since I thought the question asked for another variable (newValue) to be added into the loop. If somebody is stuck because of this it is not the case fyi