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

Jeongeui Ji
Jeongeui Ji
3,761 Points

I can't retrieve each value of the array by using a while-in loop... and then I have to add some values to it...

in the array, there are a few numbers and the question is to retrieve each value of it, using a while-in loop. But I don't know how to do it... Did I miss something??

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

// Enter your code below
while counter < numbers.count {
  print(numbers[counter])
  counter++
}

while 

1 Answer

Oliver Duncan
Oliver Duncan
16,642 Points

This challenge asks you to find the sum of the numbers in the given array. Instead of printing the numbers to the screen, add them to the sum variable.

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] // another way of saying sum = sum + numbers[counter]
  counter++
}
Jeongeui Ji
Jeongeui Ji
3,761 Points

Thank you very much, Oliver! I finally got it!!

Jeongeui Ji
Jeongeui Ji
3,761 Points

Can you see my help request again? I did exactly what you did in your answer. Of course, I understood why it has to go that way. But I didn't get it right... it is so frustrating now... Please, help me again... Thanks!