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 Collections and Control Flow Control Flow With Loops Working With Loops

3 Answers

Daniel Turato
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

Hey,

Everything you have done is correct and should work the way the tasks requests for you to do. The only issue is the line:

sum = sum + counter

You're correctly calculating the sum value within the loop but using this line afterwards will actually increase the sum incorrectly and not actually using the numbers in the numbers list.

Hope this helps, Daniel

gabrielgoth
gabrielgoth
1,055 Points

Thanks for the response

Still not sure how to pull the values from the numbers array however

Daniel Turato
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

Well, you're already pulling the values from the numbers array with this line:

sum += numbers[counter]

The counter variable acts as index value for the list. So, by putting the counter value inside brackets after the numbers variable list, it will take out the value at the "counter" position. For example, If counter was = 3 then numbers[counter] would = 16. Also, by looping, the counter value is increasing each time so each value will be different as you're moving along the list.

gabrielgoth
gabrielgoth
1,055 Points

Thanks for the response again.

Still don't get how to do the second part of the code challenge though.

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.

Daniel Turato
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

This is how it works, step by step.

  • 1) Firstly, you set up all the variables which in this case is numbers, sum & counter
  • 2) The challenge wants you to calculate the sum of each value in the numbers list so the easiest way to this is with a loop. Therefore, you create a loop that can only go up to the amount of elements in the numbers list
  • 3) Now, you want to calculate the sum of all values. To do this, you need to extract the current index that the loop is currently at. You do this with the counter variable and do as I explained earlier. To make sure you remember this value, you add it to the current value in the sum variable.
  • 4) To keep the loop reaching and end, you need to make sure the counter value increases so you add 1 to it.
  • 5) The process repeats until the loop has reached its end.

Here is how the sum value will look from start to finish: 0->2->10->11->27->31->34->43

And heres how the counter value will look from start to finish: 0->1->2->3->4->5->6