Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift 2.0 Collections and Control Flow Control Flow With Loops Working with Loops

Yingzhou Xia
PLUS
Yingzhou Xia
Courses Plus Student 475 Points

I dont know how you extract the number constant array from the number constants to apply to the newValue

Can some one please explain this thank you ! I am lost, how do you implement the while loop extracting the array and set it to "newValue"

while.swift
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter<numbers.count{counter++}
sum += 
// Enter your code below

1 Answer

Tobias Helmrich
Tobias Helmrich
31,601 Points

Hey Yingzhou,

you don't necessarily have to use another variable/constant like newValue . All you have to do is get each item of the numbers array which you can do by accessing it with the counter variable as an index inside of brackets. You then have to add each item's value to the sum variable.

Like so:

let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter < numbers.count {
    sum += numbers[counter]
    counter++
}

I hope that helps! :)

Jordi Gámez
Jordi Gámez
3,568 Points

Hi Tobias,

Remember to use counter += 1 instead of counter++ because it is going to be deprecated from Swift 3.0.

Regards