Simple Game

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Hi guys,
I was really bored on day (just formatted, had no programs :nuts:), and I made this really simple JavaScript game. For some reason, it does not work in FireFox...
I suspect that it is the event declaration at last line. When I put my script through validation, it says Im missing the } after it. When I put "Main()" instead, it says that $("Timer") is null, which it should not be. It works perfectly in IE. Is FireFox holding some new event standard I should know about?

Code:
current_test = 1, num_correct = 0;
num = Math.round(Math.random() * 9);
function Main(a) {
 if (!document.getElementsByTagName) return;
 if (a==null) {
  var buttons = document.getElementsByTagName("input");
  for (a=0; a<buttons.length; a++) {
   if (buttons[a].id.indexOf("testbutton") == -1) continue;
   buttons[a].onclick=Check;
  } Timer();
 }
 if (a==2) {
  Timer();
 }
}

ctest="test1";
function NextTest() {
 if (!document.getElementById) return false;
 var newtest = "test" + (parseInt(ctest.charAt(4)) + 1);
 $(ctest).style.display="none";
 $(newtest).style.display="block";
 ctest = newtest;
 Main(parseInt(ctest.charAt(4)));
 return true;
}

t = false, hrs = "0", min = "0", sec = "0";
function Timer(x) {
 if (x==true) {window.clearTimeout(t); return true;}
 else if (x==false) {
  window.clearTimeout(t);
  sec = 0, min = 0, hrs = 0;
  return true;
 }
 if (!document.getElementById) return false;
 var timer = $("timer").firstChild;
 sec++;
 if (sec>=60) {sec = 0; min++;}
 if (min>=60) {min = 0; hrs++;}
 if (sec<10 && sec.toString().length==1) {sec = "0" + sec;}
 if (min<10 && min.toString().length==1) {min = "0" + min;}
 var time = hrs+":"+min+":"+sec;
 timer.nodeValue = time;
 t = window.setTimeout("Timer();",1000);
}

function $(id) {
 return document.getElementById(id);
}

max = 2;
function Check() {
 if (!document.getElementById) return false;
 if (num_correct>=max) {alert("You won moron!"); return true;}
 var id = new String(this.id);
 if (id.charAt(10) == num) {
  if (num_correct==(max-1)) {
   num_correct++;
   Refresh();
   Timer(false);
   NextTest();
  } else {num_correct++; Refresh();}
 } else {
  if (num_correct) {
   if (!num_correct) return;
   num_correct -= 1;
   Refresh();
  } alert("Wrong number.");
 }
 num = Math.round(Math.random() * 9);
 return true;
}

function Refresh() {
 $("numcorrect").firstChild.nodeValue = (num_correct + " of " + max);
 num = Math.round(Math.random() * 9);
}

function Hint() {
 alert("Hint: " + num);
}

window.onload=Main;

~Twinkie
 
Last edited:
Top