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 While and Repeat While

Brian Patterson
Brian Patterson
19,588 Points

Don't how to retrieve the array.

Don't know how to complete the second part of the challenge!

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 + numbers
    counter++

}

I am really struggling with this section. I am find the challenges very confusing and not very clear. I did not have this problem with the course on Swift 1.2 course!

Stephaughn Alston
Stephaughn Alston
463 Points

I'm right there with you. Sometimes the code challenges can be very confusing and misleading.

Kristen Turner
Kristen Turner
925 Points

Yes, I was completely stuck here. I had no idea where to begin.

9 Answers

Patrick Donohoe the problem is in

sum = sum + numbers

here you would be trying to add the value of the sum added to an entire array, which could not be done. We must use indexes to point which element in the array we want.

In this particular example, we want to add each value of array's element to the var sum, so we get the total sum of array's value. We also have variable counter which will it value will increment by 1 each time in the loop until it's value less by 1 than the length of the array.

so we should write:

while counter < numbers.count  {
       sum = sum + numbers[counter]
//the counter represent the index. so first loop it will be numbers[0] which will add
//the value 2, next loop number[1] which will add value 8 (sum value here = 0+2+8)
//.. all the way to number[6].
    counter++

}

read the comment up there to understand more, and you may need to review the array course again.

hope this helped you.

I find the exercices and the tutorial quite confusing, and often either referring to irrelevant part of previous tutorials (todo list for a number of items for ex), and or assuming all this is really basic for a novice, also sorry but the examples given (airplane buckle etc) are simply confusing, I'd rather understand the logical setup of a case in which this would actually be useful

thanks for the great tut am really trying to give constructive feedback and not just be annoying.

isaac brakha
isaac brakha
6,781 Points

i thought it was only me, i never had these problems in the swift basic course with Amit. This guy makes the lessons way more complicated than they should be.

Alysa Wakefield
Alysa Wakefield
689 Points

agreed. There are some big steps missing in between the tutorial and the challenge you're expected to do. it just makes me feel useless and disheartened that i haven't understood something that hasn't been taught to me yet.

very confused on this as well ! the video did not show any of this.. at least for the second part...

Oscar Navarro
Oscar Navarro
420 Points

It gives the impression that the challenges are designed to frustrate you. It is not enough to give an eight minute lecture and then usher the student into a code challenge. The lectures have to be followed by at least four or five example exercises, they need not be videos, just a page with step by step instructions that walk you through each individual exercise. This is not just my opinion, but a proven teaching method. These missing exercises are exactly where the noob, like me, will grasp the material.

It’s a perplexing business model that instead of enticing the student to gladly pay the subscription fee, manages to drive the would be programmer to walk away from the keyboard in a murderous mood.

Jeff Kriz
Jeff Kriz
2,690 Points

So how would you compute the sum for part 2 of the challenge? This is a tough challenge and I think I'll be going back to do some review.

Patrick Donohoe
Patrick Donohoe
2,135 Points

Could you please post your answer.

good job. In my case, it was also confusing. But in a different way. I submitted task 1, which was correct. But at task 2 I was wondering what I am still missing. It looked correct. So I submitted the code and I again passed. Somehow I already wrote the code for task 2 in task 1 lol.

Stephaughn Alston
Stephaughn Alston
463 Points

You mind posting the code... I'm a little confused.

Rafael Fu
Rafael Fu
3,045 Points

This one is so confusing. I considering going back.

Stephaughn Alston
Stephaughn Alston
463 Points
sum = sum + numbers[counter]
numbers[0]
numbers[1]
numbers[2]
numbers[3]
numbers[4]
numbers[5]
numbers[6]
counter++
nikhil chandra
nikhil chandra
6,803 Points

I think it's a great way of approach, rather than simply saying its confusing. i main important thing about coding is "Do as much as u can". he doesn't want to spoon-feed, he want to make us think how the logic is working on. So, my suggestion please work rather than explaining self that we cant understand

Gino Graziano
Gino Graziano
901 Points

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

Anthia Tillbury
Anthia Tillbury
3,388 Points

The second code challenge was the first time I hit the wall.

I was close, but couldn't do it.

Thanks for everybody's help!