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

Thanapat Wongchalermthan
Thanapat Wongchalermthan
4,749 Points

Can I know the answer of this question

I dont know what wrong with my code please help me figure it out

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

while sum <= counter{
    for counter in numbers{
        sum = sum + counter

    }
    print(sum)
    sum
}

1 Answer

Jeff McDivitt
Jeff McDivitt
23,970 Points

There are several issues with your code for this challenge

  1. You have less than or equal to when the while loop just requires less than
  2. The task does not ask for a for loop or print statement
  3. You do not have counter incrementing in your while loop
  4. The task does not ask you to add the sum to counter
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below

while counter < numbers.count {

sum += numbers[counter]
counter += 1

}