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 2.0 Collections and Control Flow Control Flow With Loops Working with Loops

Lewis Mansbridge
Lewis Mansbridge
1,119 Points

Hi there, was wondering if i could have some help with some while loops, thank you

I'm really struggling with these loops, has anyone got a guide of some sort of contact i can ask them a few things about, thank you for your help!

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

// Enter your code below

while sum < counter {
  numbers // do i add numbers from the Number array up ? CONFUSED ! :( 
  counter++
} 

// THANKS!

2 Answers

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

I believe you need to change your while statement to something like while counter < 6 /* can also do: while counter < length(numbers) { sum = sum +numbers[counter]; }

Im not in this class so you will need to verify the length function name: it may be len(). I think the point of the exercise is to add all the numbers up and keeping adding until there are no more numbers. At that point the loop needs to quit.