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

can u help me with this exercise please

i dont understand what they ask in the exercise

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

// Enter your code below
while counter < numbers.count { counter += 1}

while counter < todo.count {
  counter += 1
  print(sum += counter)

}

1 Answer

Hey Carlos,

You've got this!!

You know you need to use a while loop and keep looping until counter is about to reach the same value as numbers.count. Perfect.

Inside the loop you need to increment counter else we'll be here forever! Again, you know to do that; counter += 1.

The thing you've not got quite right is adding the values of each of the numbersarray into sum. You want to access the value held in the array at the position in the array determined by counter so, like numbers[counter]. You don't want to print anything so leave that method alone.

Add that value into sum. Something like sum += numbers[counter] <- does that make sense to you? We're adding the value held at the counter position inside the numbers array into the cumulative value being gathered in sum.

The finished code looks like:

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

I hope that helps. Shout if not.

Steve.