Connecting Tech Pros Worldwide Help | Site Map

simple example not working or is there something missing?

Newbie
 
Join Date: Oct 2006
Posts: 16
#1: Jan 26 '07
here is a code from a book.

<html>
<body>

<script type="text/javascript">

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

function functionname(parameter-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??
Expert
 
Join Date: Oct 2006
Location: NC
Posts: 1,722
#2: Jan 26 '07

re: simple example not working or is there something missing?


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
Newbie
 
Join Date: Oct 2006
Posts: 16
#3: Jan 26 '07

re: simple example not working or is there something missing?


Quote:

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.
Reply