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 trialKris Larsen
8,015 PointsAdd a value to sum
I have been sitting with this task for a while now, but I can't figured out. What am I missing?
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.
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < numbers.count {
print(numbers[counter])
counter++
}
sum =
4 Answers
Steve Hunter
57,712 PointsHi Kris,
Let's take this a step at a time, just like in the question.
Step 1: 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)
That looks like:
while (counter < numbers.count){
counter++
}
That gives us the basics - a loop that'll keep going until all the elements of the array have been exhausted.
Step 2: Using the value of counter as an index value, retrieve each value from the array and add it to the value of sum.
sum += numbers[counter]
And that's it!
So, the while loop iterates as many times as there are elements in the array. Inside each loop, the value of the next element is added to the sum
variable. Nothing ever outputs this value, disappointingly!
The final code looks like:
while (counter < numbers.count){
sum += numbers[counter]
counter++
}
I hope that helps.
Steve.
Eli Rivera
946 Pointscounter ++ didn't work for me... in swift 3, I believe the code would look like this:
while counter < numbers.count { sum += numbers[counter] print(sum) counter += 1 }
Steve Hunter
57,712 PointsHi Eli,
Yes, in Swift 2.0 which was current in 2015 when this thread was created, the unary operator ++
was valid syntax. Swift 3.0 is different in many ways. One way, in particular, is that the unary operator ++
is not longer valid (neither is --
), so you need to use the binary operator +=
or go "long-hand" with a x = x + 1
construct.
There's nothing "wrong" with the question, answers or comments in this thread - just the language changed through its constant development. Swift 3 is a different beast to Swift 2.
Steve.
Linus Karlsson
7,402 PointsActually this answer did it for me, but I don't understand how.
Basically, as a course/track gets more difficult I copy/paste answers from here when I cannot find out the answer for myself and I feel awful about that.
Steve Hunter
57,712 PointsWhich bit are you struggling to understand? These concepts do get easier over time as you repeat them through the track. The concept can be difficult when first introduced.
Linus Karlsson
7,402 PointsYes, Steve, I think you are absolutely right about repetition. I have to be patient with myself and my ability to learn.
One example is the "xxx.count". I was having trouble figuring that one out on my own since I forgot it from earlier. Then again I only started with Swift last week.
Patience and stubbornness is key!
Steve Hunter
57,712 PointsIf you've only been doing this a week, don't beat yourself up about not remembering everything! You're doing great!!
Linus Karlsson
7,402 PointsThank you, Steve =)
Kris Larsen
8,015 PointsThanky, now I understand :)
Steve Hunter
57,712 PointsNo problem - glad it helped.
Steve.
Nick Mauro
3,944 PointsHelped me too, thanks!
Steve Hunter
57,712 Points
Linus Karlsson
7,402 PointsLinus Karlsson
7,402 PointsNone of these answers/comments are working for me and I'm feeling increasingly stupid.
(I wonder if I'm in over my head with this programming stuff, sometimes)
Can someone explain this code challenge again for me, please?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi Linus,
can you post what code you have produced - that will help us figure out a solution for you.
The Swift language has moved on since this post was written; there's some changes made. So, if you can link to the new challenge and post your code, that would help.
Steve.