i'm having little problem here, i have got timer here that counts down from a set time, but what i want to do now is instead of having timer, i just want to make a counter so if(counter=10) stop the game, and not when time = 0

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<SCRIPT LANGUAGE="JavaScript">
var time=45;
var timesup=0;
var started=0;
function CountDown() {
if(time>0)
{document.math.timer.value=time;
time=time-1;
var gameTimer=setTimeout("CountDown()", 1000)}
else if (time==0)
{document.math.timer.value="0";
timesup=1;
alert('Time\'s Up!');
document.math.firstnum.value="";
document.math.operator.value="";
document.math.secondnum.value="";
document.math.answer.value="";
}
}

function startgame()
{if (started!=0)
{alert('You\'ve Already Started!');}
else
{
started=1;
CountDown();
getProb();
}
}

function randnum(min,max)
{
var num=Math.round(Math.random()*(max-min))+min;
return num;
}

var choose, rightanswer
function getProb()
{
choose=randnum(1,4);
if (choose=="1")
{document.math.operator.value="+";
var choose1=randnum(0,50);
var choose2=randnum(0,50);
document.math.firstnum.value=choose1;
document.math.secondnum.value=choose2;
rightanswer=choose1 + choose2;
}
if (choose=="2")
{document.math.operator.value="-";
var choose2=randnum(0,50);
var choose1=randnum(choose2,50);
document.math.firstnum.value=choose1;
document.math.secondnum.value=choose2;
rightanswer=choose1 -  choose2;
}
if (choose=="3")
{document.math.operator.value="x";
var choose1=randnum(0,10);
var choose2=randnum(0,10);
document.math.firstnum.value=choose1;
document.math.secondnum.value=choose2;
rightanswer=choose1 * choose2;
}
if (choose=="4")
{document.math.operator.value="/";
var choose2=randnum(1,10);
var choose1=choose2 * randnum(0,10);
document.math.firstnum.value=choose1;
document.math.secondnum.value=choose2;
rightanswer=choose1 /  choose2;
}
}
function answerit()
{
if (started==0)
{alert('You Must Click The Button Labeled \'Start\'!');}
else
{
if (timesup!=0)
{alert('Time Ran Out!');}
else
{
var theiranswer=eval(document.math.answer.value);
var theirpoints=eval(document.math.points.value);
if (theiranswer==null)
{alert('Put Your Answer In The Box To The Left Of The Button Labeled \'Submit Answer\'!');
document.math.answer.select();}
else
{
if (theiranswer==rightanswer)
{
alert('Right');
theirpoints++;
document.math.points.value=theirpoints;
}
else
{alert(theiranswer + " is Wrong!\n\n"+rightanswer + " is the correct answer!")}
document.math.answer.select();
getProb();
}
}
}
}
</script>
</head>

<body>
<table align="center">
  <tr>
    <td colspan="3" align="center"><input type="button" value="New Game" onClick="location.reload();"></td></tr><tr>
    <td colspan="3" align="center"><input type="button" value="Start" size="5" onClick="startgame()"></td>
  </tr>
  <tr>
    <form name="math">
      <td colspan="3" align="center"><font face="Arial, Helvetica, Sans Serif" size="3" color="#0000FF"><b>Time Remaining:</b> </font><input type="text" name="timer" size="5"></td>
    
    <tr>
      <td align="right"><font face="Arial, Helvetica, Sans Serif" size="3" color="#0000FF"><b>Solve:</b> </font><input type="text" name="firstnum" size="5"></td>
      <td align="center"><input type="text" name="operator" size="2"></td>
      <td align="left"><input type="text" name="secondnum" size="5"></td>
    </tr>
    <tr>
      <td colspan="3" align="center"><hr><font face="Arial, Helvetica, Sans Serif" size="3" color="#0000FF"><b>Put Answer Here:</b></font> <input type="text" name="answer" size="5"> <input type="button" value="Submit Answer" onClick="answerit();"><hr></td>
    </tr>
    <tr>
      <td  colspan="3" align="center"><font face="Arial, Helvetica, Sans Serif" size="3" color="#0000FF"><b>Points:</font><input type="text" name="points" size="5" value="0"></td>
    </tr>
    <tr>
    </form>
  </tr>
</table>
</body>
</html>

Recommended Answers

All 3 Replies

Member Avatar for Pnorq

You mean like declare a global in the JavaScript and increment on each choice or set to zero at the beginning of a new game instance?

var counter = 0;

function incCounter() {
   counter++;
   if( counter > 9 ) {
     alert("Game over, dude.");
     counter = 0;
     // restart game or something
   }
}

what i want is person starts a game, no times is on.. after he presses start button a equation is given (random) as he answers it(right/wrong) it counts this entry, and another question is given and so on until 10 questions have been answered

Member Avatar for Pnorq

Well, this version gives you 10 guesses...not sure what to do at that point though. I call ResetGame() in the incCounter function (line 130)...
Search through the code for "counter" and you see the additions I put in.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<style>
<!--
.Answer		{font-weight:bold; font-size:20px; text-align:center; text-decoration:none;}
.Question	{font-weight:bold; font-size:40px; text-align:center;}
.Sign		{font-weight:bold; font-size:30px; text-align:right; font-family:helvetica;}
.High		{font-weight:bold; font-size:15px; text-align:center; }
.sect1		{background-color:#efe9df;}
.sect2		{background-color:#efefef;}
.sect3		{background-color:#e9efdf;}
.sect4		{background-color:#dfe9ef;}
-->
</style>


<center>
<form name=Flash>
<font class=Sign>Math game</font><br>
<table border=0 cellpadding=2 cellspacing=0 width=480>
<tr class="sect1" onclick="if (gamestart){alert('Not during a game.  \nStop the game first.   ');}">
	<th colspan=2>Choose difficulty<br>
	<select name="diff">
		<option value=20	setting="Easy">		Easy (0-20)
		<option value=35	setting="Medium">	Medium (0-35)
		<option value=50	setting="Hard">		Hard (0-50)
		<option value=100	setting="Expert">	Expert (0-100)
	</select>
	</th>
	<th colspan=2>Choose Math function<br>
	<select  name="func" onclick="if (gamestart){alert('Not during a game.  \nStop the game first.   ');}">
		<option	value="Add.">		Addition
		<option	value="Subt.">		Subtraction
		<option	value="Mult.">		Multiplication
		<option	value="Rnd ą">	Random (+  and -)
		<option	value="Rnd All">	Random all (+, -, and x)
		
	</select>
	</th>
</td>
<tr class="sect1">
	<td width=120><center><input type=button value="New player" onclick="ResetGame();"></td>
	<td width=120><center><input type=button value="Start game" onclick="StartGame();" style="background-color:#77dd77;"></td>
	<td width=120><center><input type=button value="Stop game" onclick="StopGame();" style="background-color:#dd7777;"></td>
	<td width=120><center><input type=button value="Instructions" onclick="window.location='#inst'"></td>
</tr>

<tr class="sect2">
	<td rowspan=3> </td>
	<td> </td>
	<td><div id="Q1" class="Question"> </div></td>
	<td rowspan=4> </td>
</tr>
<tr class="sect2">
	<td><div id="Sign" class="Sign"> </div></td>
	<td><div id="Q2" class="Question"> </div></td>
</tr>
<tr class="sect2">
	<td colspan=2><hr noshade width=75% align=right size=5 color=black></td>
</tr>
<tr class="sect2">
	<td colspan=2> </td>
	<td><center><font class="Question">?</font><br><br></td>
</tr>
<tr class="sect2">
	<td><div id="Ans1" class="Answer"> </div></td>
	<td><div id="Ans2" class="Answer"> </div></td>
	<td><div id="Ans3" class="Answer"> </div></td>
	<td><div id="Ans4" class="Answer"> </div></td>
</tr>
<tr class="sect2">
	<th><input type=button value="Answer A" id="A1" guess="" onclick="guessThis(this.guess)"></th>
	<th><input type=button value="Answer B" id="A2" guess="" onclick="guessThis(this.guess)"></th>
	<th><input type=button value="Answer C" id="A3" guess="" onclick="guessThis(this.guess)"></th>
	<th><input type=button value="Answer D" id="A4" guess="" onclick="guessThis(this.guess)"></th>
</tr>
<tr class="sect2">
	<td colspan=4><div id="Response" class="Answer"> </div></td>
</tr>

<tr class="sect3">
	<td colspan=4 valign=bottom><b>Player scoring:</td>
</tr>
<tr class="sect3">
	<td align=right>Name:</td>
	<td colspan=3><div id="Player" class="Answer"> </div></td>
</tr>
<tr class="sect3">
	<td align=right>Correct:</td>
	<td colspan=3><div id="Correct" class="Answer">0</div></td>
</tr>
<tr class="sect3">
	<td align=right>Incorrect:</td>
	<td colspan=3><div id="Missed" class="Answer">0</div></td>
</tr>
<tr class="sect3">
	<td align=right>Accuracy:</td>
	<td colspan=3><div id="Accuracy" class="Answer">0%</div></td>
</tr>
<tr class="sect3">
	<td align=right>Speed:</td>
	<td colspan=3><div id="Speed" class="Answer">0 sec</div></td>
</tr>

<tr class="sect4">	<th colspan=4>- - - - - - Highscores - - - - - -</th>
</tr>
<tr class="sect4">	<td><div id="HighName" class="High"><u><font color=#aa0000>Name</font></u><br></div></td>
	<td><div id="HighDiff" class="High"><u><font color=#aa0000>Difficulty</font></u><br></div></td>
	<td><div id="HighAcc" class="High"><u><font color=#aa0000>Accuracy</font></u><br></div></td>
	<td><div id="HighSpeed" class="High"><u><font color=#aa0000>Speed</font></u><br></div></td>
</tr>
</table>

<a name=inst></a>



</form>


<script>
var counter = 0;

function incCounter() {
   counter++;
   if( counter > 9 ) {
     alert("Game over, dude.");
     counter = 0;
     ResetGame();
   }
}

function BadBrowser () {
alert("           * * * * * A T T E N T I O N * * * * * \n\nThis page will not work with your browser.  \n\nUse either Netscape 6x or Internet Explorer 5x or higher.    ");
}

if (document.layers) {navigator.family = "nn4"; BadBrowser();}
if (window.navigator.userAgent.toLowerCase().indexOf('opera') != -1) {navigator.family = "opera"; BadBrowser();}
if (document.all) {navigator.family = "ie4"}
if (window.navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {navigator.family = "gecko";
	document.Flash.diff[0].setting = "Easy";
	document.Flash.diff[1].setting = "Medium";
	document.Flash.diff[2].setting = "Hard";
	document.Flash.diff[3].setting = "Expert";
	}
if (window.navigator.platform.toLowerCase().indexOf('mac') != -1) {navigator.OS = "mac";}

farray = new Array("+","-","*","+ -","+ - *");
fSignarray = new Array();
gamestart=false;
named=false;

good = new Array("Correct!","You got that one right.","Another one right.","You're doing great!",
"You're going to the head of the class!","I knew you could get that one.","That was easy, wasn't it?",
"That's the right answer.","Good going!","");

bad = new Array("Oops, try again.","Better luck next time","Try a different answer.","Nope, not that one.",
"nope, that one was a little hard.","Hmmm, that is incorrect.","Nope, try again.");

function ResetGame() {

counter = 0;

name=prompt("Enter your name.","");
if (name == "") {alert("Please enter your name or click on 'Cancel'.   "); ResetGame();}
else if (name == "null") {return;}

WriteMessage("Player" , name);
WriteMessage("Correct" , "0");
WriteMessage("Missed" , "0");
WriteMessage("Accuracy" , "0%");
WriteMessage("Speed" , "0 sec");
Timer = new Array();
named=true;
WriteMessage("Response" , "<font color=#0000aa>Click on 'Start game' to begin.");
}


function StartGame() {

counter = 0;

if (!named) {
	WriteMessage("Response" , "<font color=#0000aa>Click on 'New player' and enter your name first.");
	return;
	}
if (gamestart) {return;}
WriteMessage("Correct" , "0");
WriteMessage("Missed" , "0");
WriteMessage("Accuracy" , "0%");
WriteMessage("Speed" , "0 sec");
Timer = new Array();
gamestart=true;
WriteMessage("Response" , "<font color=#0000aa>Click on an answer button.");
showProblem();
}

function StopGame(){
if (!gamestart) {return;}
else {	if (window.navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {navigator.family = "gecko";}
	if (navigator.family == "ie4") {
		WriteMessage("HighName" , Player.innerHTML +"<br>","add");
		WriteMessage("HighDiff" , document.Flash.diff[document.Flash.diff.selectedIndex].setting+" "+ document.Flash.func[document.Flash.func.selectedIndex].value +"<br>","add");
		WriteMessage("HighAcc" , Accuracy.innerHTML +" ("+ Correct.innerHTML +"/"+ (Math.floor(Correct.innerHTML)+Math.floor(Missed.innerHTML)) +")<br>","add");
		WriteMessage("HighSpeed" , Speed.innerHTML +"<br>","add");
		WriteMessage("Response" , "<font color=#0000aa>Look at your Highscore!");	
		gamestart=false;
		}
	else if (navigator.family == "gecko") {

		WriteMessage("HighName" ,	document.getElementById("Player").innerHTML +"<br>","add");
		WriteMessage("HighDiff" ,	document.Flash.diff[document.Flash.diff.selectedIndex].setting +" "+ document.Flash.func[document.Flash.func.selectedIndex].value +"<br>","add");
		WriteMessage("HighAcc" , 	document.getElementById("Accuracy").innerHTML +" ("+ document.getElementById("Correct").innerHTML +"/"+ (Math.floor(document.getElementById("Correct").innerHTML)+Math.floor(document.getElementById("Missed").innerHTML)) +")<br>","add");
		WriteMessage("HighSpeed" ,	document.getElementById("Speed").innerHTML +"<br>","add");
		WriteMessage("Response" , 	"<font color=#0000aa>Look at your Highscore!");	
		gamestart=false;
		}
	}
}


function showProblem () {
StartTime = new Date().getTime();
if (document.Flash.func.selectedIndex < 3) {
	MathSign = farray[document.Flash.func.selectedIndex];
	}
else {	RSign = farray[document.Flash.func.selectedIndex].split(" ");
	MathSign = RSign[Math.floor(Math.random() * RSign.length)];
	}
num1 = Math.floor(Math.random() * document.Flash.diff[document.Flash.diff.selectedIndex].value);
num2 = Math.floor(Math.random() * document.Flash.diff[document.Flash.diff.selectedIndex].value);
if (MathSign == "-") {
	if (num1 < num2) {
		temp=num1;
		num1=num2;
		num2=temp;
		}
	}

WriteMessage("Q1" , num1);
WriteMessage("Q2" , num2);
if (MathSign == "*") 	{WriteMessage("Sign", "x");}
else			{WriteMessage("Sign", MathSign);}

CorrectAns = eval(num1 + MathSign + num2);

fakie=",";
for (x=1;x<5;x++) {
	if (MathSign == "+")	{fakeans = Math.floor(Math.random() * (document.Flash.diff[document.Flash.diff.selectedIndex].value*2));}
	if (MathSign == "-")	{fakeans = Math.floor(Math.random() * document.Flash.diff[document.Flash.diff.selectedIndex].value);}
	if (MathSign == "*")	{fakeans = Math.floor(Math.random() * document.Flash.diff[document.Flash.diff.selectedIndex].value) * Math.floor(Math.random() * document.Flash.diff[document.Flash.diff.selectedIndex].value);}
	if (fakie.match(","+fakeans+",") || fakeans == CorrectAns) {x--;}
	else {	fakie += fakeans+",";
		WriteMessage("Ans"+x , fakeans);
		if (navigator.family == "ie4") {document.all["A"+x].guess = fakeans;}
		else if (navigator.family == "gecko") {document.getElementById("A"+x).guess = fakeans;}
		}
	}

CPick = Math.floor(Math.random() * 3)+1;
WriteMessage("Ans"+CPick , CorrectAns);
if (navigator.family == "ie4") {document.all["A"+CPick].guess = CorrectAns;}
else if (navigator.family == "gecko") {document.getElementById("A"+CPick).guess = CorrectAns;}

}

function guessThis (number) {
if (!gamestart) {
	WriteMessage("Response" , "<font color=#0000aa>Click 'Start game' first.");
	return;
	}
	
	incCounter();
	
if (number == CorrectAns) {
	EndTime=new Date().getTime();
	Timer[Timer.length] = EndTime-StartTime;
	WriteMessage("Response" , "<font color=#00aa00>"+ good[Math.floor(Math.random() * (good.length-1))]);
	if (navigator.family == "ie4") 		{WriteMessage("Correct" , Math.floor(Correct.innerHTML)+1);}
	else if (navigator.family == "gecko") 	{WriteMessage("Correct" , Math.floor(document.getElementById("Correct").innerHTML)+1);}
	updateScore();
	showProblem();
	}
else {	WriteMessage("Response" , "<font color=#aa0000>"+ bad[ Math.floor(Math.random() * (bad.length-1))]);
	if (navigator.family == "ie4") 		{WriteMessage("Missed" , Math.floor(Missed.innerHTML)+1);}
	else if (navigator.family == "gecko") 	{WriteMessage("Missed" , Math.floor(document.getElementById("Missed").innerHTML)+1);}
	updateScore();
	}
}


function updateScore () {
if (navigator.family == "ie4") 		{
	Perc = (Math.floor(Correct.innerHTML ) / (Math.floor(Missed.innerHTML )+ Math.floor(Correct.innerHTML))) * 100;
	}
else if (navigator.family == "gecko") 	{
	Perc = (Math.floor(document.getElementById("Correct").innerHTML ) / (Math.floor(document.getElementById("Missed").innerHTML )+ Math.floor(document.getElementById("Correct").innerHTML))) * 100;
	}
WriteMessage("Accuracy" , Perc.toString().substring(0,4) +"%");
temptimer = 0;
for (x=0;x<Timer.length;x++) {
	temptimer += Math.floor(Timer[x]);
	}

newSpeed = (temptimer*.001/(Timer.length)).toString().substring(0,(temptimer*.001/(Timer.length)).toString().indexOf(".")+3);
if (newSpeed != "Na") {
	WriteMessage("Speed" , (temptimer*.001/(Timer.length)).toString().substring(0,(temptimer*.001/(Timer.length)).toString().indexOf(".")+3) +" sec");
	}
}


function WriteMessage(where,what,addto) {
if (window.navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {navigator.family = "gecko";}
if (addto) {
	if (navigator.family == "ie4")		{document.all[where].innerHTML += what;}
	if (navigator.family == "gecko")	{document.getElementById(where).innerHTML += what;}
	}
else {
	if (navigator.family == "ie4")		{document.all[where].innerHTML = what;}
	if (navigator.family == "gecko")	{document.getElementById(where).innerHTML = what;}
	}
}

</script>


</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.