Jump to content

"For" loops within classes JS w3School do not work?


JKowalski
 Share

Recommended Posts

Hello, to my experience the following does not work (tryit-editor w3School), but it does with a "while" loop instead. Are "for" loops not allowed within classes?


<p id="demo">result</p>

class Test{

  counter(){
    var result=0;
    for(i=0;i<10;i++){
      result++;
    }
    return result;
  }

  counter2(){
    var result=0;
    while(result<10){
      result++;
    }
    return result;
  }

}

var obj=new Test();
document.getElementById("demo").innerHTML=obj.counter();
//document.getElementById("demo").innerHTML=obj.counter2();

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...