83.7k views
2 votes
Help I don’t know why it’s not compiling

public class g
{
// instance variables - replace the example below with your own
function roll ()
{
return Math.floor (Math.random()*4+1);
}
int a;
function buttonPressed(){
roll();
a=roll();
answertq=(x*y);

}
{
if(parseInt(document.getElementById("inputVal").value)==answertq)
{
if (a==1)
{
window.alert("very good!");
}
else if (a==2)
{
window.alert ("excellent");
}
else if (a==3)
{
window.alert("correct, nice work.");
}
else if (a==4)
{
window.alert ("correct - keep up the good work");
}
}
}

Help I don’t know why it’s not compiling public class g { // instance variables - replace-example-1
User Stojke
by
7.3k points

1 Answer

3 votes

Answer:

It is not entirely clear what your program is trying to achieve, but I embedded it inside some HTML to show you a working version.

  • class methods in javascript don't require the 'function' keyword.
  • to access class variables, always use the 'this' instance reference.
  • when using class methods as event handlers, bind them to 'this'. This is a weird javascript quirk you want to check out.

Help I don’t know why it’s not compiling public class g { // instance variables - replace-example-1
User Mariachimike
by
7.2k points