www.webdeveloper.com
Results 1 to 4 of 4

Thread: Onload Event

  1. #1
    Join Date
    Feb 2008
    Posts
    23

    Onload Event

    With this program, if the user gets the answer correct they will get a alert saying good. However I need an prompt to appear right after the alert asking the user if they would like to answer another questions. which the user will type yes or no. Yes, it will execute again, no it will end. (Posted a thread before on this but it was resolved, this is a new problem)

    Any help on this would be great!
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>DS</title>
    
    <script LANGUAGE = "JavaScript">
    function begin() 
    { var value, value2, answer, input;
    
    x = functionValue();
    y = functionValue();
    answer = x * y;
    
    while(input!=answer){
    
    //Get user input
    input = parseFloat(window.prompt("How much is "+x+" times "+y+"?", "0"));
    
    if(input==answer){
    alert("Good!");}
    
    else
    alert("Wrong");}
    
    
    } 
    
    function functionValue()
    { 
    
    return Math.floor( 1 + Math.random() * 9 );
    
    }
    
    </SCRIPT>
    </HEAD>
    <BODY ONLOAD="begin()">
    </BODY>
    </HTML>
    Last edited by Kor; 03-19-2009 at 08:50 AM. Reason: wrap the code [code][/code]

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Two versions for you (I personally like the confirm option better), note that x, y and answer need to be re-defined inside the while loop or it will constantly ask the same exact thing:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>DS</title>
    
    <script type="text/javascript">
    function begin() {
        var value, value2, answer, input = 'yes', x, y; //input needs a string definition prior to the while loop in this case
        while(input.toLowerCase() !== "no"){
            //Get user input
            x = functionValue();
            y = functionValue();
            answer = x * y;
            input = window.prompt("How much is "+x+" times "+y+"?", "0");
            if(Number(input) == answer){
                alert("Good!");
            } else {
                alert("Wrong");
            }
            input = window.prompt("Would you like to answer another question? (yes or no?)");
        }
    } 
    
    function functionValue(){ 
        return Math.floor( 1 + Math.random() * 9 );
    }
    
    </SCRIPT>
    </HEAD>
    <BODY ONLOAD="begin()">
    </BODY>
    </HTML>
    The following version uses a confirm box instead (a bit tidier than forcing another user input):

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>DS</title>
    
    <script type="text/javascript">
    function begin() {
        var value, value2, answer, input, x, y, con = true; //con needs pre-definition of true
        while(con){
            //Get user input
            x = functionValue();
            y = functionValue();
            answer = x * y;
            input = window.prompt("How much is "+x+" times "+y+"?", "0");
            if(Number(input) == answer){
                alert("Good!");
            } else {
                alert("Wrong");
            }
            con = confirm("Would you like to answer another question?\n(o.k. to continue or cancel to exit)"); //returns true on o.k. or false on cancel
        }
    } 
    
    function functionValue(){ 
        return Math.floor( 1 + Math.random() * 9 );
    }
    
    </SCRIPT>
    </HEAD>
    <BODY ONLOAD="begin()">
    </BODY>
    </HTML>

  3. #3
    Join Date
    Feb 2008
    Posts
    23
    Thanks for the help!! That help out so much!!

    I was wondering,

    Heres the code that I got from your example, I wanted to know How can I make it so that the user will go back to the same question if they got the answer wrong.


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>ds</title>
    
    <script type="text/javascript">
    function begin() {
    var value, value2, answer, input = 'yes', x, y; //input needs a string definition prior to the while loop in this case
    while(input.toLowerCase() !== "no"){
    //Get user input
    x = functionValue();
    y = functionValue();
    answer = x * y;
    input = window.prompt("How much is "+x+" times "+y+"?", "0");
    if(Number(input) == answer){
    alert("Very Good!");
    input = window.prompt("Would you like to answer another question? (yes or no?)");
    } 
    
    else {
    alert("No. Please try again");
    }
    
    }
    } 
    
    function functionValue(){ 
    return Math.floor( 1 + Math.random() * 9 );
    }
    
    </SCRIPT>
    </HEAD>
    <BODY ONLOAD="begin()">
    </BODY>
    </HTML>

  4. #4
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    I wanted to know How can I make it so that the user will go back to the same question if they got the answer wrong.
    Add in a boolean variable - in this case, newQuestion - and redefine x, y and answer only if newQuestion is true:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>ds</title>
    
    <script type="text/javascript">
    function begin() {
        var value, value2, answer, input = 'yes', x, y, newQuestion = true; //input needs a string definition prior to the while loop in this case
        while(input.toLowerCase() !== "no"){
        //Get user input
            if (newQuestion) {
                x = functionValue();
                y = functionValue();
                answer = x * y;
            }
            input = window.prompt("How much is "+x+" times "+y+"?", "0");
            if(Number(input) == answer){
                alert("Very Good!");
                input = window.prompt("Would you like to answer another question? (yes or no?)");
                newQuestion = true;
            } else {
                alert("No. Please try again");
                newQuestion = false;
            }
    
        }
    } 
    
    function functionValue(){ 
    return Math.floor( 1 + Math.random() * 9 );
    }
    
    </SCRIPT>
    </HEAD>
    <BODY ONLOAD="begin()">
    </BODY>
    </HTML>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles