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 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry Having trouble declaring the function twice

    I have a quick question. I'm writing a piece of code that will generate if it is an odd number or an even number. I was able to get the output. Now I'm trying to figure out how can I declare the function twice. This is my code:

    function f(n)
    {
    document.write(n);


    }

    var m=Math.round(1+ Math.random()*100);

    if (m%2!==0)
    f(m + " is an odd number<br>");
    else
    f(m + " is an even number<br>");

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    No, you cannot have multiple scripts with the same names of functions and/or variables.

    If you require a second instance of the same script you must rename all functions and variables to (say) m2 and so on.

    Your code has several errors:


    Code:
    var m = Math.round(1+ Math.random()*100);
    if (m&#37;2 != 0) {
    var f = (m + " is an\nodd number");
    }
    else {
    f = (m + " is an\neven number"); 
    }
    
    alert (f)
    Not equal to is != not !==
    Newline is \n not <br> which is a literal, but newline is not required here.


    If at first you don't succeed, redefine success.
    Last edited by Philip M; 10-14-2008 at 05:46 PM.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,378
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by Philip M View Post

    Not equal to is != not !==
    Newline is \n not <br> which is a literal, but newline is not required here.
    actually not equal is !==, not equivalent is !=.

    although all it really needs is the condition changed to if(m&#37;2). no reason to compare to zero here...



    OP:
    not sure what you mean by "declare the function twice".
    you could wrap your loose code in a function, at which point it can be called repeatedly:
    Code:
    function f(n){document.write(n);}
    
    function reportEveness(m){
      if (m%2){
         f(m + " is an odd number<br>");
      }else{
         f(m + " is an even number<br>");
      }
    }
    
    
    
    // do two different numbers:
    
    var n=Math.round(1+ Math.random()*100);
    reportEveness(n);
    
    n=Math.round(1+ Math.random()*100);
    reportEveness(n);
    Last edited by rnd me; 10-14-2008 at 07:21 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #4
    New Coder
    Join Date
    Oct 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile

    Thank you kindly I was able to figure out the coding. I meant to say that I would like to run the function twice so that way the results from the random function appears differently rather than the same.


  •  

    Posting Permissions

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