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 trialThanapat Wongchalermthan
4,749 PointsCan I know the answer of this question
I dont know what wrong with my code please help me figure it out
let numbers:[Int] = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while sum <= counter{
for counter in numbers{
sum = sum + counter
}
print(sum)
sum
}
1 Answer
Jeff McDivitt
23,970 PointsThere are several issues with your code for this challenge
- You have less than or equal to when the while loop just requires less than
- The task does not ask for a for loop or print statement
- You do not have counter incrementing in your while loop
- The task does not ask you to add the sum to counter
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < numbers.count {
sum += numbers[counter]
counter += 1
}
Thanapat Wongchalermthan
4,749 PointsThanapat Wongchalermthan
4,749 PointsThank you very much for your help!