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

Hurkan Dogan
Hurkan Dogan
8,099 Points

Counter Issue

I am making counter +=1 and it compiles as while condition but as numbers[] index value it is not taking the value. Numbers[counter] stucks with 0!

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

// Enter your code below
var arrayValue = numbers[counter]

while counter <= numbers.count {

sum = sum + arrayValue
counter += 1
}

4 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points
  1. It did not ask you to create a variable arrayValue, that is not needed
et numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

while counter < numbers.count {

    sum += numbers[counter]
    counter += 1

}
Hurkan Dogan
Hurkan Dogan
8,099 Points

Yes but with this way counters in numbers[] is not working. It stucks with 0 and not moving. And while loop sums 7 times 2. It is also in Xcode with this way.

Jeff McDivitt
Jeff McDivitt
23,970 Points

Are you referring to this challenge because this is exactly what it is asking you to do. If you are attempting something on a personal project; please describe what you are trying to do and I will see if I can assist you with it

Hurkan Dogan
Hurkan Dogan
8,099 Points

Thank you jeff. It did worked out!

In my first time I didn"t create any value and I did the same like you!

I don't know what happened in first time I've tried but I think I lost something in my code.

Thank you for helping.