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 trialNacho Sepulveda
2,203 Pointsi have no idea on how to retrieve data from an array and sum it
fd
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < 6 {
counter++
}
1 Answer
jcorum
71,830 PointsSeveral things. You started the while loop correctly, but never, ever want to hard-code in the length of the array, as you code would then give the wrong result if anyone added values to the array. Next, you need to use the loop counter variable counter to access the various values in the array. Third, you need to accumulate those values by adding them to the sum variable. Finally, as you were doing, you need to increment counter each time through the loop.
while counter < numbers.count {
sum += numbers[counter]
counter++
}