JKowalski Posted May 23, 2020 Share Posted May 23, 2020 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 More sharing options...
Ingolme Posted May 23, 2020 Share Posted May 23, 2020 It has an error because you forgot to declare i with the var keyword. Link to comment Share on other sites More sharing options...
Sherin Posted May 25, 2020 Share Posted May 25, 2020 You have not declare I with datatype of int ,var then you facing error in for loop Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now