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

Pierre Robert
Pierre Robert
5,717 Points

Stuck at Collections and Control Flow challenge 2 of 2

Below is the challenge:

This one might be a bit tricky so let's take it in steps. We have an array of numbers, nothing tricky, you've seen this before. What we want is to compute the sum of the numbers in the array. To do this we're going to use a while loop. 
We have a variable ,sum, that will store the value of the sum of numbers from the array. 
We also have a variable ,counter, which we will use to track the number of iterations of the while loop. 
Step 1: Create a while loop. The while loop should continue as long as the value of counter is less than the number of items in the array. (Hint: You can get that number by using the count property)

My first answer is OK:

while sum < numbers.count { sum++ print(numbers) }

Step 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.

I'm lost.

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

// Enter your code below

while sum < numbers.count {
    sum++
    print(numbers)
}

5 Answers

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Pierre Robert !

Your first answer was wrong - I don't now why that passed because you have to use counter as counter variable.

It should look like this (Challenge Task 2 of 2):

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++
}

Hope that helps :)

don't forget ; before counter++

Clinton Rupp
Clinton Rupp
1,645 Points

I have to say that this section was incredibly frustrating...I wound up watching these videos multiple times only to encounter the same issues with no clear way to progress. It's particularly frustrating that these code challenges come with no teachers' notes, and no explanation for why/how the correct answer was achieved, or how/why the code challenges relate to the material covered in the videos.

Bridget Farmer
Bridget Farmer
2,833 Points

agree - I've been enjoying this course up til now. Feel like this section jumps some logic. It's like - here's how you add a and b, and now for the code challenge please add up alligators and bears. I know it's meant to test what we've just learned but it's hard to connect the two.

Daniel Walker
Daniel Walker
2,031 Points

This worked for me.

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

}
Pierre Robert
Pierre Robert
5,717 Points

Hi Holger!

Thank you for your answer. I too don't understand why my original answer was accepted. And since I still don't know how to deal with challenge 2 of 2, I will go through the tracks again, from the top. It will be a good refresher and I have time.

Many thanks, Pierre

I have to agree with everyone that the code challenges are incredibly intricate with such simple explanations in the videos. I feel as though the code challenges should be more geared toward the difficulty addressed in the actual video tutorials. Agreed - this is incredibly frustrating not having the proper tools.