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

Need help for challenge task 2 of 2 on the section swift collections and control Flow, please help.

Now that we have the while loop set up, it's time to compute the sum! Using the value of counter as an index value, retrieve each value from the array and add it to the value of sum.

For example: sum = sum + newValue. Or you could use the compound addition operator sum += newValue where newValue is the value retrieved from the array.

I've seen plenty answers from those who have asked the same question before and none of the answers worked for me.

ex:

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

or

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

}

or

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

or

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

none of these worked when I entered them to submit the challenge...

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.count)
counter += 1
}

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

Hi Vanessa, I just completed this myself just a second ago. Their is no need to have two while loops running, you only need one.

Your second while loop would work but your formatting is off. You opened a parentheses then closed it with a bracket. It should look something like this:

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

Hope this helps!

Brady Wyniemko thank you!

1 Answer

Forest Zemel
PLUS
Forest Zemel
Courses Plus Student 550 Points

So I'm stuck on this as well and to be honest, I don't remember anything about the code in this 2nd task. I'm wondering if I missed a video. When did Pasan EVER go over this?

For this video I have, in my notes, how to print an array, ranges, while loops, and repeat while loops.

I see and remember nothing about computing the sums. Then again, Pasan is no where near as descriptive or thorough as any other instructors at Treehouse and tends to fast forward through virtually everything.