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

Hussein Kheireddine
seal-mask
.a{fill-rule:evenodd;}techdegree
Hussein Kheireddine
Front End Web Development Techdegree Student 4,151 Points

How would I format this next portion of the challenge in order to compute the sum?

I understand that I am trying to compute the sum of all the numbers in the array, however I'm having trouble figuring out exactly how I should format this. How would I format the newValue?

loops.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 += 1
}

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

1 Answer

Hussein Kheireddine
seal-mask
.a{fill-rule:evenodd;}techdegree
Hussein Kheireddine
Front End Web Development Techdegree Student 4,151 Points

Actually, I just figured it out! By using a For in Loop, which looks like this:

for newValue in numbers { sum += newValue print(sum) }

Thank you very much for at least looking at this, and I hope that this is something that can help anyone else who struggled with this challenge like I did.