Noob help: Function problem.

Discussion in 'JavaScript' started by Xenonsoft Studios, Jun 30, 2009.

  1. #1
    I'm new to JavaScript and learning it currently. I decided I would set myself a test, see if I can create a formula that calculates and displays all the prime numbers under 100.

    I've got that working fine, but when I put it into a function, linked in with an input button it doesn't want to work.

    The script (placed in the header currently) is as such:

    <script type="text/javascript">
    function myfunction()
    {
    var a, b, c, d;
    for (num=1; num<=100; num++)
    {
    // Creating variables that hold the remainder when num is divided by these 5 numbers
    a=num%2; b=num%3; c=num%5; d=num%7;
    // If the number is 2, 3, 5 or 7 write it now
    if (num==2 || num==3 || num==5 || num==7)
    {
    document.write(num + ", ");
    }
    // If the remainders of all aren't 0 or if the number isn't 1 then write it out as a prime
    else if (a!=0 && b!=0 && c!=0 && d!=0 && num!=1)
    {
    document.write(num + ", ");
    }
    }
    }
    </script>


    But really, as I know the script itself works, you could ignore it and just concentrate on:

    <script type="text/javascript">
    function myfunction()
    {
    SCRIPT
    }
    </script>


    Whilst in the body I've got:

    <form>
    <input type="button" value="Yeah!" onclick="myfunction()" />
    </form>


    It's probably a simple problem, but as someone knew to it I'm not sure why it wont work.

    Currently when the button is clicked, it disappears and nothing else appears.

    Any helps appreciated.
     
    Xenonsoft Studios, Jun 30, 2009 IP
  2. pipisdicelana

    pipisdicelana Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Your code works, check the rest html code you probably have error there
     
    pipisdicelana, Jun 30, 2009 IP
  3. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #3
    Yes, it works absolutely fine.
    May be you forgot to place it inside a head tag.

    <html>
    <head>
    <script type="text/javascript">
    function myfunction()
    {
    var a, b, c, d;
    for (num=1; num<=100; num++)
    {
    // Creating variables that hold the remainder when num is divided by these 5 numbers
    a=num%2; b=num%3; c=num%5; d=num%7;
    // If the number is 2, 3, 5 or 7 write it now
    if (num==2 || num==3 || num==5 || num==7)
    {
    document.write(num + ", ");
    }
    // If the remainders of all aren't 0 or if the number isn't 1 then write it out as a prime
    else if (a!=0 && b!=0 && c!=0 && d!=0 && num!=1)
    {
    document.write(num + ", ");
    }
    }
    }
    </script>
    </head>
    
    <body>
    <form>
    <input type="button" value="Yeah!" onclick="myfunction()" />
    </form>
    </body>
    </html>
    Code (markup):
     
    Unni krishnan, Jun 30, 2009 IP