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 trialJosef Dosch
2,619 PointsHaving trouble accessing value of array and add it to "sum/newValue"
I need to use counter as an index. access the array and add the new value to sum. feel like i tried everything and nothing is working.
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < 7 {
print(numbers)
counter++
}
1 Answer
Ali Amin
1,295 PointsAs the hint says you can use the count
property in Array
to get the number of items in you array instead of counting them
while counter < numbers.count
What if you don't have access to the array? (if it was passed to you in a function or populated via a for loop) it is always better to use the solution that will work under any conditions.