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

Dawson Young
Dawson Young
512 Points

Screw this I STILL CANT GET IT TO WORK

I am so done with this challenge I'm trying to learn stuff but I've been stuck on this garbage for like a week now. Can someone just tell me the answer and what exactly to type in

while.swift
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

while sum < 7 {
print (numbers)
sum++
}

sum += numbers[counter] 
counter++

2 Answers

Luke Dawes
Luke Dawes
9,739 Points

Hi Dawson,

The first part of the Code Challenge asks you to put together a while-loop. The loop needs to run for every item in the numbers array, which you can access using numbers.count. You'll also need to make sure that each time the loop runs, that counter variable increments by 1, so you don't accidentally write an infinite loop.

let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below
while counter < numbers.count {
counter++
}

What we're saying is, while the counter variable is less that the number of items in the numbers array (accessed using numbers.count), continue running the loop and increment that counter variable each time through.

The second part of the Code Challenge asks you to add every number in the numbers array together and store that in the sum variable. I took their suggestion and declared a new variable, newValue, that would use array index notation to access whatever item in the numbers array we were up to in our loop. It would then add that to the sum variable in the way you did it before, only you'll need to put it inside the loop so it does this each time.

I left the incrementation of counter until the end of the loop, because that should be the last thing it needs to do before running again over the next item in numbers.

let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below
while counter < numbers.count {
    let newValue = numbers[counter]
    sum += newValue
    counter++
}

That should pass. Good luck!

Dawson Young
Dawson Young
512 Points

Thank you so much Luke. You have explained this in a way I understand, the cloud has been lifted. One thing I didn't know I could do was but new variables in a loop but I guess you can, sweet. Again, thanks a lot bro I appreciate it!!!

Luke Dawes
Luke Dawes
9,739 Points

No problem, mate!

Another way to do it without needing to declare a new variable inside the while loop could be like this:

while counter < numbers.count {
    sum += numbers[counter]
    counter++
}

Keep at it.