Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Need Help on this function ()

    Helloo all of javascript's Guru,

    why i couldn't have z as x*y ?

    Thank you


    <script type = "text/javascript">
    var x,y,z;


    document.write("How much is " + getRandom(x) + " times " + getRandom(y) +"<br\>");
    document.write("The answer is " + getRandom(z) );

    function getRandom()
    {

    x = Math.floor( 1 + Math.random() * 9);
    y = Math.floor( 1 + Math.random() * 9);
    z = (x * y);
    return x,y,z;

    }

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The function is fine other than it returns too many values. Take out the whole return line in the function.

    Then call the function before the document.write lines like

    getRandom();

    Finally since getRandom() doesn't return anything anymore and since it has already been called to set the values x, y, and z, change "getRandom(x)" to just "x" and the same with y and z in the document.write lines.

    That should work. This sounds like it might be homework so no I'm not going to do it for you, but I am willing to give pointers when I have the time and inclination.

    Good luck with it.

    david_kw

  • #3
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you. i forgot i can use getRandom() instead of using getRandom(x) or something.

    anyway, this is not a homework. i just got bored and try to learn javascript.

    Thank you again david. It's work now
    Last edited by Mewanto; 12-14-2006 at 08:14 PM.

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A worthy goal.

  • #5
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This what i am currently making.

    When i have answered a correct answer and generate new question. why there are not have any inputVal ?.

    Help please .. Pointer should be enough.. so i can imporve my self

    Can i load "checkAnswer" again?. so i can have inputVal again.

    Thank you in advance


    <!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=iso-8859-1" />
    <title>Function Random Number</title>
    <script type = "text/javascript">
    var x,y,z;


    getRandom();
    document.write("How much is " + x + " times " + y +"<br\>");
    document.write("The answer is " + z );


    function getRandom()
    {
    x = Math.floor( 1 + Math.random() * 9);
    y = Math.floor( 1 + Math.random() * 9);
    z = x* y
    return x,y,z;
    }

    function getNewQuestion()
    {
    getRandom();
    document.write("How much is " + x + " times " + y +"<br\>");
    document.write("The answer is " + z );
    }

    function checkPressed()
    {

    var check = checkAnswer.inputVal.value;
    var checkValue;

    checkValue = parseInt( check );

    if (checkValue != z)
    window.status = "No. Please try again.";

    else
    {
    window.status = "Very good";

    }

    if (checkValue == z)
    getNewQuestion();
    }

    </script>
    </head>


    <body>
    <form name = "checkAnswer" action = "">
    <p> Enter The Answer <br/>
    <input name = "inputVal" type = "text"/>
    <input name = "Enter" type = "button" value = "search"
    onclick = "checkPressed()" /><br /></p>

    </form>
    </body>

    </body>
    </html>
    Last edited by Mewanto; 12-15-2006 at 05:28 AM.

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    One problem I see. This

    var check = checkAnswer.inputVal.value;

    should be

    var check = document.checkAnswer.inputVal.value;

    david_kw

  • #7
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you david_kw.

    It's still a problem - i cant get back the input textbox -.-.

    i will try again. if you have any pointers, please let me know ya. Pointer should be enough, so i can learn to understand.

  • #8
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When you do document.write() after the inital load it will rewrite over everything in the page. It can be useful when the page is building but document.write isn't very useful once the page has been loaded.

    Code:
    function newWrite(n, s) {
       document.getElementById("writediv" + n).innerHTML = s;
    }
    .
    .
    .
    getRandom();
    newWrite(1, "How much is " + x + " times " + y);
    newWrite(2, "The answer is " + z );
    .
    .
    .
    <div id="writediv1"> </div>
    <br />
    <div id="writediv2"> </div>
    This is untested but it might give you an idea how else it could be done.

    david_kw

  • #9
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you . i figured it out..


  •  

    Posting Permissions

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