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 Pointsdon't know how to retrieve the value from the array and add it to the sum.. my newValue statement is definitely wrong ha
Thank you
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter < numbers.count {
(numbers[counter])
counter++ }
var newValue = counter + numbers
sum = sum + newValue
// Enter your code below
ben sasser
476 Pointsto retrieve the values from the numbers array and go up by 1 index each time???
Kyler Smith
10,110 PointsClose! In order to retrieve it, you would be needing to store it in a variable though (maybe the variable 'sum'? :) ). Try adding that to sum.
sum += numbers[counter]
ben sasser
476 Pointsstill doing something wrong.. Sorry for being so bad haha. Here is the new code :
let numbers = [2,8,1,16,4,3,9] var sum =0 var counter = 0
while counter < numbers.count { (numbers[counter]) counter++ }
sum += numbers[counter]
var newValue = counter + numbers
sum = sum + newValue
Kyler Smith
10,110 PointsWe all started somewhere! Those last 2 lines can go because they are not needed in the calculation.
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter < numbers.count {
/* (numbers[counter]) this is what you originally had.
All you needed to do was add that to sum each time you went through the loop*/
sum += numbers[counter]
counter++
}
/* sum += numbers[counter]
this line goes inside of the body of your while loop.
It needs to be after the first bracket '{' but before the 'counter++'.
Because you are using counter as an index,
you don't want to increment it before you can grab the value from the array. */
// var newValue = counter + numbers
// sum = sum + newValue
1 Answer
Jennifer Nordell
Treehouse TeacherAs I posted earlier, this is the entirety of the code needed:
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while counter < numbers.count {
sum += numbers[counter]
counter++
}
ben sasser
476 Pointssigh haha. maybe I'm just tired... thank you guys for the help.
Kyler Smith
10,110 PointsKyler Smith
10,110 PointsIn the body of your while loop you have the line that reads
(numbers[counter])
what purpose do you believe it serves?