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

While Loops 2 of 2 Challenge

Have exhausted all possibilities with this challenge; including those suggested in the community and not getting through the challenge...All works fine in my playground! Here is my answer:

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

Help!

Challenge: Using the value of counter as an index value, retrieve each value from the array and add it to the value of sum.

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

Part 1: while counter < numbers.count { print (numbers[counter]);counter++ }

Nathan Tallack
Nathan Tallack
22,164 Points

Can you link to the challenge for me?

6 Answers

Hello Rosana:

I am not sure 100% what is going on with your code, or what you are missing. But it looks Ok to me, I went to the challenge and did it myself to compare it to yours.

Here is my code

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

// Enter your code below

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

This passes just fine. Perhaps you deleted something by accident ? give it another try see if it works.

Nathan, Elaborated on detail. Thanks for your help.

Jhoan, My thoughts exactly. Still not working...

Would you please tell me what is it telling you ?

"Make sure your condition allows for iteration over all elements in the numbers array and check your calculation of sum!"

I have no clue what is going on.. It could be the compiler. Have you tried closing your browser and trying it again ?

Multiple times Jhoan! :D It's quite the head scratcher, isn't it...

Try copying and pasting my code into the challenge.. delete the one they give you.

Deleting the previous code worked. Thanks for the tip! :D Woot, woot.

lol welcome, glad you can move on now..

I had to do the same! Absolutely ridiculous waste of time!!!