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 trialJazil Zaim
8,502 PointsHow do I solve this?
I am supposed to retrieve each value from the array and add it to the value of sum.
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < numbers.count {
var newValue = counter + sum
sum = sum + newValue
counter += 1
print(numbers[count])
}
1 Answer
Dan Lindsay
39,611 PointsHey Jazil,
Use the counter value as the index value of the numbers array, then assign that to your new value. You don't need the print statement either. Just this:
while counter < numbers.count {
var newValue = numbers[counter]
sum += newValue
counter += 1
}
First time through the loop, counter equals 0, so numbers[counter] will be 2 the first time through. End of the loop we increment counter by one, so the second time through numbers[counter] will be 8, and so on.
Hope this helps!