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 trialabdirahman liban
Courses Plus Student 2,163 Pointsi need help
i couldn't solve it
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
var newValue = numbers
// Enter your code below
while sum < 2 {
print( sum + newValue)
sum++
}
1 Answer
Michael Reining
10,101 PointsHi,
The conditions for the while loop in your code are wrong.
The while loop should run until you have looped through all of the numbers in the array. To do that it should look like this.
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
var newValue = numbers
// Enter your code below
while counter < numbers.count { // while the counter is less than the number of items in the array run the loop
counter++ // increment the counter
}
In part 2, they are asking you to compute the sum. For that, just keep adding the numbers from the array like this.
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
var newValue = numbers
// Enter your code below
while counter < numbers.count {
sum = sum + numbers[counter] // access number in array by using the counter
counter++
}
I hope that helps,
Mike
PS: Thanks to the awesome resources on Team Treehouse, I launched my first app.
Now you can practice writing Swift code directly on your iPhone :-)
Code! Learn how to program with Swift
https://itunes.apple.com/app/code!-learn-how-to-program/id1032546737?mt=8