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

William Hartvedt Skogstad
William Hartvedt Skogstad
5,857 Points

Working with Loops, Challenge Task 2 of 2

I can't figure out what they want me to do in this code challenge, how should I approach this?

Challenge Task 2 of 2

Now that we have the while loop set up, it's time to compute the sum! Using the value of counter as an index value, retrieve each value from the array and add it to the value of sum.

For example: sum = sum + newValue. Or you could use the compound addition operator sum += newValue where newValue is the value retrieved from the array.

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 {
   sum = sum + newValue
   counter++
}

15 Answers

  1. "Using the value of counter as an index value, retrieve each value from the array"- so you want to set up the numbers array in the loop to evaluate at each index (i.e. numbers[0], numbers[1], etc.) as shown below:
numbers[counter]
  1. and add it to the value of sum. - easy enough, add to the previous sum and assign that to the new sum.
sum = sum + numbers[counter]

Each time the loop evaluates a index value of the numbers array, it will grab the value (numbers[0] = 2), add the previous sum (no previous sum in number[0] so 2+0) and add it to the new sum (2 + 0 = 2 <- the new sum). Then that repeats for each of the index values in the numbers array.

Full code should be:

while counter < numbers.count {
    sum = sum + numbers[counter]
    counter++
}
William Hartvedt Skogstad
William Hartvedt Skogstad
5,857 Points

Thanks again for the detailed description! It really helps me understand everything better :)

Ashvin Mbuguni
Ashvin Mbuguni
6,360 Points

I feel that this swift course isn't very good, because I've encountered content that hasn't been taught and the format of questioning in numerous challenges are not very clear. Treehouse need to fix this ASAP!

Josue Gisber
PLUS
Josue Gisber
Courses Plus Student 9,332 Points

your code looks good, the only thing you have to change is the newValue variable for number[counter]

Shandas Duchintav
Shandas Duchintav
8,222 Points

urgh I still can't get this one right. The code above doesn't work.

William Hartvedt Skogstad
William Hartvedt Skogstad
5,857 Points

Show me your code and I might be able to help.

William Humphrey
William Humphrey
5,543 Points

For Swift 3 C++ has been removed and replaced with +=; therefore let numbers = [2,8,1,16,4,3,9] var sum = 0 var counter = 0 while counter < numbers.count { sum = sum + numbers[counter] counter += 1 }

I couldn't figure this out and in the solution provided above, I find this line a little confusing. Can you please clarify this? Specifically what we're doing with numbers[counter]?

  sum = sum + numbers[counter]
William Hartvedt Skogstad
William Hartvedt Skogstad
5,857 Points

What you're doing is setting a new value for "sum" to the total of "sum" + the value of each index in the "numbers" array. This will cause the var "sum" to display all the numbers in the "numbers" array as one total.

Example:

let numbers = [1, 2]
var sum = 0
var counter = 0


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

// The var "sum" should now display the value 3

This specific "while loop" performs the addition of the numbers in the "numbers" array while the var "counter" is less than the number of ints in the "numbers" array (hence the "numbers.count).

I hope this helped :)

Josue Gisber
PLUS
Josue Gisber
Courses Plus Student 9,332 Points

sum = sum + numbers[counter], this is telling the compiler: **Get whatever number is in the array numbers and add that number to the variable sum. ** So every time the while loop goes, it grab a number from the array and add that number to whatever number exist in the sum variable. i hope this make sense.

Shandas Duchintav
Shandas Duchintav
8,222 Points

I got it. Thanks for the reply!

Matthew Corry
PLUS
Matthew Corry
Courses Plus Student 4,389 Points

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

Paschal Ihenacho
Paschal Ihenacho
914 Points

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

Paolo Di Donato
Paolo Di Donato
4,945 Points

My current code is below and I am finding an error still. I have even copied and pasted what someone posted as the correct answer... Help is appreciated!

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

// Enter your code below

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

Jos Feseha
Jos Feseha
4,011 Points

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

}

Jos Feseha
Jos Feseha
4,011 Points

This is the write answer

Paolo Di Donato
Paolo Di Donato
4,945 Points

Right, thank you so much.. devils in the details

Eddie Flickinger
Eddie Flickinger
3,513 Points

The answer passes, but will never run through on the play ground. I keep getting type 'Int' has no subscript members

I Feel somethings havent been taught prior to code challenge -- i haven't seen ++ not a single time

  • this is frustrating af