Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Collections and Control Flow Control Flow With Loops Working With Loops

compute sum

Sorry. I am trying to understand but I am stuck. I do not even know what to do and where to start. And I do not understand what happened, when I got lost. I tried to read https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID120 documentation. however it starts from IF and I do not even know what it is. Guys, I really do not like to ask for a help, but I am lost. Can you tell me where I can read more on loops?? I do not understand the material at all, watching the lessons more than 2-3 times did not help, it is still not clear.. ((

loops.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 += numbers[counter]
  counter += 1
}

sum += counter

Please, anyone....

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Eugene St,

Are you on the second part of the challenge? What is the question asking?

If you're on part one, this code should be correct.

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

As for reading material on loops/control flow, the Apple Programming Language guide is the best resource.

Good Luck

Yep, the second part of the task. Here it is:

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.

By the way, I am glad to see you again:) you are my star. It would be great if you can reach me directly at eugene.namecheap@gmail.com , I am newby in IOS coding but I work hard your help would be highly appreciated ;) Any personal tasks or help or advices...

Steven Deutsch
Steven Deutsch
21,046 Points

So it looks like I misunderstood the first part of the question and ended up solving both tasks at stage one. The same code should pass.

As for the Apple Language Guide goes, you don't have to read it in order but you can and I recommend it. You will learn about conditional statements on treehouse after this section on loops. In Apple's guide, on the other hand, they introduce conditional statements before loops.

Steven Deutsch
Steven Deutsch
21,046 Points

Thank you for the kind words. My advice would be expose yourself to new things, read the Swift Programming Language Guide, code every day if even for 15 minutes. If you have any questions just post them on the Treehouse forums.

Thanks Steven.

So it seems that my code was correct. I just had to remove the last line.. Thanks for your attention to this case. I also appreciate your recommendation. I will spend the next couple of hours to read Apple documentation and then return to Treehouse course, hopefully it will be more clear.

Enjoy your day!

Steven Deutsch
Steven Deutsch
21,046 Points

I would recommend supplementing what you do on Treehouse with the Swift Language Guide. After learning a topic on Treehouse, read the corresponding section in the Swift Language Guide.

Roger, much appreciated!