simple example not working or is there something missing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jasonchan
    New Member
    • Oct 2006
    • 16

    simple example not working or is there something missing?

    here is a code from a book.

    <html>
    <body>

    <script type="text/javascript">

    /* Paramater-Passing Basics
    A function's basic syntax:

    function functionname(pa rameter-list)
    {
    statements
    }

    */

    function message(name)
    {
    if (name != "")
    alert("Hello there "+name);
    else
    alert("Don't be shy");
    }

    message("Jason" );
    message();

    </script>

    </body>
    </html>



    basically, the empty function --> message(); is supposed to prompt the alert message "Don't be shy". Instead, it says hello there undefined.

    based on the book, it says that calling the function either message(""); or without the paramater, message(); should prompt this message. it works only for message(""); and not without the parameter. ... whats goin on here??
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    The function message() expects the parameter name therefore when you don't pass it a value the message() is undefined. Add message("Bob") to the second call and you will see what I mean. If you pass the function an empty string message("") that is what it will return.

    HTH,
    Aric

    Comment

    • jasonchan
      New Member
      • Oct 2006
      • 16

      #3
      Originally posted by AricC
      The function message() expects the parameter name therefore when you don't pass it a value the message() is undefined. Add message("Bob") to the second call and you will see what I mean. If you pass the function an empty string message("") that is what it will return.

      HTH,
      Aric
      yea thats what i thought. but the author says that the function message() SHOULD prompt the same thing as message(""). That's the confusion that I have.

      Comment

      Working...