Fizz Buzz Loading Issue

When I enter this code:

// Add your code below:
var counter = 1

while counter <= 100 {
  for counter in 1...100 {
    print(counter)
  }
}

It’s not printing and I don’t know why.

@trk075,

You are not incrementing your counter variable.

Remember, the word in between for and in is just a placeholder. Just because you also call it “counter”, it doesn’t mean that it is the same as your counter variable.

So it should be something like a letter instead.

It doesn’t need to be, it can be anything. You also don’t need both the for loop and the while loop to make this work. You can use both if you want, but either one will get the job done. Just remember that if you want your while loop to ever stop, you need to make sure you increment counter until it reaches 101…

I’ve tried to do that but it always says its trying to connect to codecademy.

That has nothing to do with your code. That means your connection with the site timed out or was lost. Try refreshing the page and make sure you look for the little green dot near the top of the screen where it should say “Connected”.

Its completely fine on other codes but it seems to not work on this project for some reason.

You should see this at the top of the screen:
image

If you don’t, you need to refresh the page.

Once the page is refreshed, make sure you change your code before trying to hit run. You likely got booted off because your while loop caused an infinite loop (since you never incremented counter to something greater than 100)

I removed the while loop and it finally worked.

Awesome. So you got it working with just a for loop - do you understand how to make it work with a while loop?

I just tried it now and I have to say, no I don’t.

Alright, so the thing to remember with while loops is that they run forever while their condition is true.

In this code:

var counter = 1

while counter <= 100 {
  print(counter)
}

… the while loop would never end because counter never changes — it always remains 1, and thus, counter <= 100 stays true. This is known as an infinite loop, and it will crash your program (or browser or computer). When you use a while loop, you have to be extra careful to ensure the condition eventually becomes false.

In this case, all you need to do is add 1 to counter on each iteration of the loop. You could do this by saying counter += 1:

var counter = 1

while counter <= 100 {
  print(counter)
  counter += 1
}

By doing this, counter continues growing until it reaches 101, at which point it ends your while loop.
You might want to practice this in a few different ways just to make sure you have a grasp on it.

Let me know if you have any more questions — happy coding! :slight_smile:

1 Like

I finally was able to use both the while and for loops together without running into the stupid reconnecting issue.

But that makes no sense. It will amount to 10000 iterations.

for x in 1...100 {

}

should be enough to do what you want it to do.

I tried that, and it didn’t work. I got this error:

FizzBuzz.swift:5:7: warning: immutable value 'x' was never used; consider replacing with '_' or removing it
  for x in 1...100 {
      ^
      _

Did you try the suggested _ in place of the x?

I did, and it surprisingly worked.

1 Like

https://learnappmaking.com/loops-swift-how-to/#loop-over-ranges-with-for-in

As the error says here, you never used x in this loop. If you said…

for x in 1...100 {
  print(x)
}

…then it would not have given you an error.

I heard you and worked as it should.

1 Like