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 trialYingzhou Xia
Courses Plus Student 475 PointsI dont know how you extract the number constant array from the number constants to apply to the newValue
Can some one please explain this thank you ! I am lost, how do you implement the while loop extracting the array and set it to "newValue"
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter<numbers.count{counter++}
sum +=
// Enter your code below
1 Answer
Tobias Helmrich
31,603 PointsHey Yingzhou,
you don't necessarily have to use another variable/constant like newValue
. All you have to do is get each item of the numbers
array which you can do by accessing it with the counter
variable as an index inside of brackets. You then have to add each item's value to the sum
variable.
Like so:
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter < numbers.count {
sum += numbers[counter]
counter++
}
I hope that helps! :)
Jordi GΓ‘mez
3,568 PointsJordi GΓ‘mez
3,568 PointsHi Tobias,
Remember to use counter += 1 instead of counter++ because it is going to be deprecated from Swift 3.0.
Regards