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

While loops in Swift

Hello! I'm stuck! Please can somebody to help me with this task:
Now that we have the while loop set up, it's time to compute the sum! Using the value of counter as an index value, retrieve each value from the array and add it to the value of sum. For example: sum = sum + newValue. Or you could use the compound addition operator sum += newValue where newValue is the value retrieved from the array.https://teamtreehouse.com/library/swift-collections-and-control-flow/control-flow-with-loops/working-with-loops

Kenan Bateman
Kenan Bateman
16,715 Points

Hi Evgenii,

I'm unsure where you might be hitting an issue without seeing your code, but the answer is

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

3 Answers

Kenan Bateman
Kenan Bateman
16,715 Points

Oh whoops my solution satisfied part 1, but to make it satisfy part 2 it'd be this --

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

For your code specifically, I think the issue is that it should be counter <=6 ....since on the 7th item, you'd be at numbers[6]

Hello Kenan! Thank you so much for your answer. This is my code: """let numbers = [2,8,1,16,4,3,9] var sum = 0 var counter = 0 while counter < 6 { sum += 1 }""" I created a loop. But i can not find the decision how to put my numbers isight of the loop that i could to sum of them. And i checked your answer, it does'not right - Bummer! Make sure your condition allows for iteration over all elements in the numbers array and check your calculation of sum!

Kenan thanks a lot for your code! Its really helped me!