Infinite Loop in a Webpage Problem

While doing the course i had a power outage in the house,
when we got the electricity back on i tried loading my website.html and it doesn’t load anymore, it technically is unresponsive.
I’m not quite sure what the issue is if i could get some clarity on this it would be great.

the code in the file:

THIS is life
<body>

    <h1>This is the title</h1>

    <script>

    var texttodisplay="hellooo";


      for (var counter = 0; counter<100; counter-=5 ){
          document.write("<h1> Another loop iteration</h>");
          document.write("<h2>Counter is now "+ counter +
          " " +texttodisplay +"</h2>");
      }

    </script>

</body>

Why are you have counter-=5?

Your code:

  1. Start with 0
  2. Count until 100
  3. Count -5

So this means you have infinite negative loop. If you will change to counter+=5 you will have 5, 10, 15…
But your code goes -5, -10, -15 and will never get to 100 because it goes negative.

1 Like

Oh well i just learned something new,
infinite loops makes web-pages unresponsive.
thanks buddy!

2 Likes

You’re welcome. And yes infinite loop can make web page and programs unresponsive because they will count forever.

2 Likes

your loop will always be true. Infinite loop. try

for (var counter = 0; counter<20; counter++ ){

The standard accepted code is to make your code readable. If you dont have basis of the counter but to reach a certain iteration, then make it straightforward.

Your loop displays 5, 10, 15, 20. You should do it like this as later on you will develop it a bad habit and very hard to trace for the next programmer to update. Important to understand is to show your direct intention.

document.write(“

Counter is now “+ (counter * 5) +” " +texttodisplay +”

");