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

Aramik YOUSEFZADEH
PLUS
Aramik YOUSEFZADEH
Courses Plus Student 895 Points

I have defined a terminating condition but I still get error, what s wrong with my code?

In the while loop I said :

while counter =< numbers.count

and also did :

counter += 1

But i still get error

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

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You simply have a typo. You've managed to transpose two characters. =< is not a valid comparison operator but <= is. If I make this teensy correction, your code passes with flying colors!

Hope this helps! :sparkles:

edited for additional note

Actually, I tried it again and was forced to change it to >= which I believe to be incorrect. I'm going to check this in playgrounds and if it is incorrect will tag the instructor.

edited again for correct answer

Besides having a syntax error by using =<, there is also a logic flaw. This loop should run only while counter is less than numbers.count. Remember that if counter goes up to the same number as the number of items it will result in an index out of range error. There are seven items in that array. But the seventh item resides at an index of 6 or numbers[6]. This means if you try and access numbers[7] you will get an out of range error as this index doesn't exist inside this array.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Pasan Premaratne could you take a peek at the challenge checker, please? These two while conditions pass where I feel like they shouldn't.

while counter > numbers.count {}
while counter >= numbers.count {}

Thanks!

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

Hey Jennifer Nordell

I get an error on

while counter >= numbers.count {}

Admittedly the error is not descriptive and can be improved but I can't get it to pass

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Pasan Premaratne I just meant the logic including the rest of the code. If I put in this challenge, it states "Well done! You're doing great!" even though the resulting value of sum would be equal to 0 as the loop is never executed. I tested it again just now to make sure.

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
}