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 trial

iOS Swift 2.0 Collections and Control Flow Control Flow With Loops Working with Loops

ben sasser
ben sasser
476 Points

don'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

while.swift
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
Kyler Smith
Kyler Smith
10,110 Points

In the body of your while loop you have the line that reads

(numbers[counter])

what purpose do you believe it serves?

ben sasser
ben sasser
476 Points

to retrieve the values from the numbers array and go up by 1 index each time???

Kyler Smith
Kyler Smith
10,110 Points

Close! 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
ben sasser
476 Points

still 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
Kyler Smith
10,110 Points

We 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

As 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
ben sasser
476 Points

sigh haha. maybe I'm just tired... thank you guys for the help.