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 trialRodrigo Alarcon
8,028 PointsSwift 3
please help me. I was out for a couple of days so i re watched the video, but i am still stuck
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter.count < numbers.count {
}
1 Answer
Moritz Lang
25,909 PointsHi, it's not that hard and you're on the right way ;)
while counter < numbers.count {
}
Because in this particular case counter
is always less the count of items in numbers
the loop would never end. Therefore you have to increment the counter each time you ran the loop by one like so:
while counter < numbers.count {
counter += 1
}
The next step would be to add each number to sum
. This is easily done by extending the current while-loop like this:
while counter < numbers.count {
sum += numbers[counter]
counter += 1
}
Here we are using the current value of counter to grab each number from the array.
Please let me know if you have any further questions :)
Rodrigo Alarcon
8,028 PointsRodrigo Alarcon
8,028 PointsThe challenge is -
In this task, we have an array of numbers and we want to compute the sum of its values.
We have a variable ,sum, that will store the value of the sum of numbers from the array.
We also have a variable ,counter, which we will use to track the number of iterations of the while loop.
Step 1: Create a while loop. The while loop should continue as long as the value of counter is less than the number of items in the array. (Hint: You can get that number by using the count property)