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 trialMaria Angelica Dadalt
6,197 PointsHow do I get the loop to sum de numbers in the array?
let numbers = [2,8,1,16,4,3,9] var sum = 0 var counter = 0
while counter < { counter+=1 }
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter < 7 {
counter+=1
}
2 Answers
Kyle Johnson
33,528 PointsHi Maria!
First, I would like to suggest google searching to try an find a solution to these treehouse challenges and any times you may get stuck writing your own code. In this case, I searched "swift array" and the first link was to the apple swift documentation. Getting familiar with the documentation is a must!
It looks like we are trying to get each value from the array. To do this we call the array, followed by brackets with the index of the value we want to get. We will use the variable counter
because this will cycle through the index values with help of the while loop.
numbers[counter]
Next we want to add all those values.
sum = sum + numbers[counter]
This line of code will go here:
while counter < 7 {
sum = sum + numbers[counter]
counter+=1
}
Maria Angelica Dadalt
6,197 PointsThanks, Kyle!! I'll be sure to check google before asking again.
Kyle Johnson
33,528 PointsYou're welcome! The treehouse community is a great resource too.