PDA

Click to See Complete Forum and Search --> : hard ass javascript question


pnj
Nov 22nd, 2000, 05:45 PM
here is my code
+++++++++++++++++++++++++++++
<HTML>
<HEAD>
</HEAD>
<BODY onLoad="javascript:add();">

<SCRIPT LANGUAGE="JavaScript">

<!--
correct=0;
wrong=0;

function random(maxValue) {
day= new Date();
hour= day.getHours();
min=day.getMinutes();
sec=day.getSeconds();
mili=day.getTime()
return(((hour*3600)+(min*60)+(sec)+mili) % maxValue);
}
function ranom(maxValue) {
day= new Date();
mil=day.getTime();
return((mil) % maxValue);
}
function add()
{
maxValue=100;
numA=random(maxValue);
numB=ranom(maxValue);
numC=numA + numB;
document.write ("type the correct answer" + numA + "+" + numB + "=") ;
document.write ("<center><form name=FrmAdder><input name='txtNum' size='5'><br><input type='button' value='add' onClick='newwin.ans();'></form></center>");
return true;
}

function ans()
{
alert("hello")
if (Answer == numC) {
correct++;
msg = "Congratulations, your answer is correct.";
}
else {
wrong++;
msg = "Oops! " + Answer + " is incorrect.\n\n"
+ "The correct answer was " +numC + ".";
}
score = "" + ((correct / (correct + wrong)) * 100);
score = score.substring(0,4) + "%";
alert(msg + "\n\nYOUR SCORE: " + score + "\n"
+ correct + " correct\n"
+ wrong + " incorrect")
}

// -->
</script>

</BODY>
</HTML>
+++++++++++++++++++++++++++++++

this pops up in a new browser and generates two random numbers and a text box and a submit button.

when the user clicks the enter button it should call the ans() funtion. but it doesn't.

any suggestions??
anyone??

strangly when I view the source of this in IE i only get the document.write code.but in Netscape I get all the code you see upabove.


???????????????

thanks for any help from anyone!

Nov 22nd, 2000, 11:52 PM
1 thing I have figured out so far.

<BODY onLoad="javascript:add();"> <-not suppose to be like that.


<BODY onLoad="add();"> <-the right way

And the reason why the code is not visible is because

<!--
// -->

^These make it so that certain browsers won't see the code.

I am still trying to figure out the rest. The window pops up now, but the button isn't doing anything :rolleyes:.

Nov 23rd, 2000, 12:42 AM
I am trying to break it apart now.

<input type='button' value='add' onClick='newwin.ans();'></form></center>");

newwin is undefined.
Do you need it?
Can you not have just 'ans();' ?

pnj
Nov 23rd, 2000, 05:18 PM
thanks for the help Matthew ,
I got the code working better.
this is what the code looks like now.
+++++++++++++++++
<HTML>
<HEAD>
</HEAD>
<BODY >

<SCRIPT LANGUAGE="JavaScript">
<!--
correct=0;
wrong=0;

function random(maxValue) {
day= new Date();
hour= day.getHours();
min=day.getMinutes();
sec=day.getSeconds();
mili=day.getTime()
return(((hour*3600)+(min*60)+(sec)+mili) % maxValue);
}
function ranom(maxValue) {
day= new Date();
mil=day.getTime();
return((mil) % maxValue);
}
function add()
{

maxValue=100;
numA=random(maxValue);
numB=ranom(maxValue);
numC=numA + numB;
document.write ("type the correct answer" + numA + "+" + numB + "=") ;
document.write ("<center><form name=FrmAdder onSubmit='ans();return false;'><input name='txtNum' size='5'><br><input type='button' value='add'onClick='ans();'></form></center>");
return true;
}

function ans()
{
var Answer = parseInt(document.forms[0].txtNum.value)

//alert("hello")test purposes
if (Answer == numC) {
correct++;
msg = "Congratulations, your answer is correct.";
}
else {
wrong++;
msg = "Oops! " + Answer + " is incorrect.\n\n"
+ "The correct answer was " +numC + ".";
}

score = "" + ((correct / (correct + wrong)) * 100);
score = score.substring(0,4) + "%";
for (i=0;i<=4;i++)
add();
alert(msg + "\n\nYOUR SCORE: " + score + "\n"
+ correct + " correct\n"
+ wrong + " incorrect")

}


add();

// -->
</script>
</BODY>
</HTML>
+++++++++++++++++++++++
I took the onLoad part out and called the funcion at the end of the script.this complete code popsup into a new window.what I meen is from the parent window,upon clicking a button, the window that opens is this code,and the window is called newwin.make sense?
so by using the newwin.ans(); I am trying to call the window itself.

You will see that I put in this code.
for (i=0;i<=4;i++)
add();
What I would like is to loop through this complete function(the add() function and the ans() function together five times)

after the user enters the math answer they get the alert and then a new question is given to them. I want the original question to disapear.

I hope that makes sense, I don't know if I explainde myself correctly the first time..

so..............
what happens now is the code will only loop twice. and the second time the add button does not function.

thanks for your help so far.

pnj

Danial
Nov 23rd, 2000, 08:19 PM
Hi Pnj,
I have just gone throuh your code, there are few things you need to think about. Like when you use document.write(), you actually create a tempory document and you lose all the javascript in your original doucument. So you cant re-use the javascript functions. They way round is using Frames, which i think is the best way. You can put all your code in the frameset and then access the functions/variables of the parent window from the child window/s using parent.functionname().

You can use the frameset as a Global Module(VB terms) to store the functions and the Score.

I have modified your code and here is the one using frames.

Hope this helps

Danial

Code for index.html


<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
correct=0;
wrong=0;

function random(maxValue)
{
day= new Date();
hour= day.getHours();
min=day.getMinutes();
sec=day.getSeconds();
mili=day.getTime()
return(((hour*3600)+(min*60)+(sec)+mili) % maxValue);
}

function ranom(maxValue)
{
day= new Date();
mil=day.getTime();
return((mil) % maxValue);
}

function add()
{
maxValue=100;
numA=random(maxValue);
numB=ranom(maxValue);
numC=numA + numB;

parent.frames["main"].document.open();
parent.frames["main"].document.write("<html><head></head><body background="/images/bground.gif" bgcolor="#ffffff" text="#000000" marginheight="0" topmargin="0">");
parent.frames["main"].document.write ("type the correct answer " + numA + "+" + numB + "=") ;
parent.frames["main"].document.write ("<center><form name=FrmAdder><input name='txtNum' size='5'><br>");
parent.frames["main"].document.write ("<input type='button' value='add' onClick='javascript: parent.ans()'>");
parent.frames["main"].document.write ("</form></center>");
parent.frames["main"].document.write("</body></html>");
parent.frames["main"].document.close();

return true;
}


function ans()
{

var Answer = parseInt(parent.frames["main"].document.forms[0].txtNum.value)

//alert("hello");
if (Answer == numC)
{
correct++;
msg = "Congratulations, your answer is correct.";
add();
}

else
{
wrong++;
msg = "Oops! " + Answer + " is incorrect.\n\n"
+ "The correct answer was " +numC + ".";

add();
}

score = "" + ((correct / (correct + wrong)) * 100);
score = score.substring(0,4) + "%";

alert(msg + "\nYour score is " + score);

}

// -->
</script>
</head>

<frameset cols="0%,*" frameborder="NO" resize=NO>
<frame src="about:blank">
<frame src="math2.html" name="main">
</frameset>

<body background="/images/bground.gif" bgcolor="#ffffff" text="#000000" marginheight="0" topmargin="0">

</body>
</html>



Code for math2.html


<HTML>
<HEAD>

</HEAD>
<BODY onload="javascript: parent.add()">


</BODY>
</HTML>

pnj
Nov 24th, 2000, 12:01 PM
I can't use frames unless I can 'hide' one of the frames so it looks like it is not frames.the reason is the client has already given me the specs on how they want this thing to function.I have never used a frameset where you can only see one frame.

basicly the main window will have four buttons.
one for add, subtract, multiply, divide.
one a button is clicked, a new window opens and the user is given one question. once they answer they get an alert box telling them they got the correct answer or incorrect answer.
then they are presented w/ a new math question in the popup window. once they have completed 5 questions, the popup window unloads and the user is again shown the origanal four math buttons.

make sense?

thanks