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 trialben sasser
476 Pointswhat does it mean by "using the value of counter as an index value"? can't figure this out
Thanks for the help!
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter < numbers.count {
(numbers[counter])
counter++ }
// Enter your code below
3 Answers
Kyler Smith
10,110 PointsYou are so close!!! It wants you to use the value of counter as the index for the numbers array. When you first hit the while loop, the value of counter is 0. To get the first value of the array, you could write
numbers[0] // this will give you 2
numbers[1] // this will give you 8
but since you need to update each time to get the next element it wants you to use counter in place of the brackets.
while counter < numbers.count {
sum += numbers[counter]
counter++
}
Good luck!
Jennifer Nordell
Treehouse Teacherben sasser I think you're doing better than you think you are! I don't even think it's possible to be closer to the solution than you are without actually getting it correct. Take a look:
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter < numbers.count {
sum += numbers[counter]
counter++ }
But let's talk about the counter and the index. Our array of numbers has indexes. They start at 0 and go up to 6. The number 2 is at index 0. The number 8 is at index 1. All the way up to index 6 which holds a 9. All we're doing is using counter to mark where we currently are looking in our array.
So while the counter is less than numbers.count (which is 7). We're going to take our sum and add the number that's in the array at the index of count. So, as stated, it's going to start at 0. And 2 is in 0. So we add 2 to sum. Then we go to 1. 8 is in 1. Now our sum is 2 + 8 which equals to 10. What this will do is get the sum of all the numbers in that array put together.
Hope this helps!
Anjali Pasupathy
28,883 PointsWhen the challenge says "using the value of counter as an index value", it means that it wants you to use counter as an index value in the numbers array. You're already doing this in the line you wrote:
(numbers[counter])
You're accessing the value in the numbers array that's held in the index represented by counter. So, when counter is 0, you're accessing the first value in the numbers array (numbers[0] -> 2). When counter is 1, you're accessing the second value in the numbers array (numbers[1] -> 8). This continues until you access the last value in the numbers array (numbers[count-1] -> 9), after which counter is increased to equal count. When counter equals count, it makes the while loop condition evaluate to false, which causes you to break out of the while loop.
This challenge also asks you to add each value in the numbers array to sum, so that once the while loop has run its course, sum is equal to the sum of all the values in the numbers array. To do this, you just need to add numbers[counter] to sum and store that value back in sum inside the while loop:
while counter < numbers.count {
(numbers[counter]) // CHANGE THIS LINE TO sum += numbers[counter]
counter++ }
I hope this helps!