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 trialEvgenii Kireev
717 PointsWhile loops in Swift
Hello! I'm stuck! Please can somebody to help me with this task:
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.https://teamtreehouse.com/library/swift-collections-and-control-flow/control-flow-with-loops/working-with-loops
3 Answers
Kenan Bateman
16,715 PointsOh whoops my solution satisfied part 1, but to make it satisfy part 2 it'd be this --
while counter < numbers.count {
sum += numbers[counter]
counter += 1
}
For your code specifically, I think the issue is that it should be counter <=6 ....since on the 7th item, you'd be at numbers[6]
Evgenii Kireev
717 PointsHello Kenan! Thank you so much for your answer. This is my code: """let numbers = [2,8,1,16,4,3,9] var sum = 0 var counter = 0 while counter < 6 { sum += 1 }""" I created a loop. But i can not find the decision how to put my numbers isight of the loop that i could to sum of them. And i checked your answer, it does'not right - Bummer! Make sure your condition allows for iteration over all elements in the numbers array and check your calculation of sum!
Evgenii Kireev
717 PointsKenan thanks a lot for your code! Its really helped me!
Kenan Bateman
16,715 PointsKenan Bateman
16,715 PointsHi Evgenii,
I'm unsure where you might be hitting an issue without seeing your code, but the answer is