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 trialVanessa Jimenez
691 PointsNeed 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...
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
}
Vanessa Jimenez
691 PointsBrady Wyniemko thank you!
1 Answer
Forest Zemel
Courses Plus Student 550 PointsSo 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.
Brady Wyniemko
476 PointsBrady Wyniemko
476 PointsHi 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!