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

Can't get through this, spent an hour on it. Really feels instructions are unclear... help please

Can't work it out

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

// Enter your code below

while counter < numbers.count {
  counter++
}

4 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

What it's asking for is for you to loop through the array that they set up at the top and figure out what all those numbers added together are. They set you up a variable named sum so that you can keep track of what the current total is. So through the first time through the loop the sum will be 2. Second time the sum will be 10... 2 + 8, The third time sum will be 11... 10 + 1. Fourth time, 27 ... 11 + 16. Take a look at the small snippet you need.

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

Here while our counter is less than the number of elements in the array we take sum and then add the number that's at the position of counter to our current running total aka sum. Hope this helps!

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Brice,

This one requires a bit of logic in the thinking process, but also have to make sure not to overthink it.

The second part of the challenge wants you to use the loop to add all the numbers in the array up. So far your code is on the right track, except you are missing the line that will calculate the sum.

We will use the counter variable to access the specific values in the array by their indexes. If this isn't familiar to you, I'd recommend going back and reviewing the video introducing Arrays.

The instructions for Task 2 gives an example of the equation you will need to use. This line of code needs to go before the counter gets incremented, so the correct index is being accessed.

Below is the corrected code for the task. I hope it makes sense once you see it. :)

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

:dizzy:

Thanks jennifer, I actually did this at the very beginning but had my counter++ before and was retrieving an error so moved away from it and spent an hour looking for other solution. really frustrating... I actually managed to do it another way but seams ur code makes way more sense than mine....

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

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

I dont see instructions. I think you need to add a statement in your while loop to add the numbers up that are in the numbers array.

sum += sum[count]

You want to add the numbers in the numbers array using indexing. Start with sum[0] all the way up to sum[6}

Cindy Lea
Cindy Lea
Courses Plus Student 6,497 Points

Im sorry. Its should be sum += numbers[counter]