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

Eunice Torres
Eunice Torres
2,278 Points

Need Swift Help!

I am trying to show the sum value based upon the array. I imputed my code in playground and it reads it out perfectly but the treehouse quiz says there is still an error, and im not sure of that error. Any ideas?

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

// Enter your code below

while counter < 6 {
     counter += 1

}     

 sum += counter

1 Answer

Hi there,

I just took the quiz and this answer passed the test based on the questions and parameters they set for you:

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

This method of solving the quiz uses the count method, which I believe is what they want - so instead of counting the number of values in an array manually, this function will determine that number for you. Also, you need to put your sum variable inside the While Loop so that it adds the values to itself each time the loop executes. The placement that you noted above will only add a single value to sum (sum will equal the final iteration of counter instead of each value of the numbers array).

I hope this helps!