Results 1 to 6 of 6

Thread: hard ass javascript question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    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!
    pnj

  2. #2
    Guest
    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 .

  3. #3
    Guest
    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();' ?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    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



    pnj

  5. #5
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    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
    Code:
    <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

    Code:
    <HTML>
    <HEAD>
    
    </HEAD>
    <BODY onload="javascript: parent.add()">
    
    
    </BODY>
    </HTML>
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    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
    pnj

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width