Jump to content
Sign in to follow this  
JKowalski

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

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();

Share this post


Link to post
Share on other sites

It has an error because you forgot to declare i with the var keyword.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...