...

View Full Version : Function Not Defined???



jrcoleman
06-19-2007, 05:48 PM
Hello All,

I am new to Javascript and am trying to understand it. I wrote a simple little script that displays a menu on the screen and then prompts the user for a choice, after they choose the selection is sent to a function which contains switch case and then the result is returned and displayed on the screen.

When I run the script I get an error (javascript console) that tells me my function is not defined? It is in the header, shouldn't it be defined? Any help identifying my error would be appreciated, I don't want to move on until I understand fully.

Here is the code:

<code>
<!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>Untitled Document</title>
<script language="javascript" type="text/javascript">

<!--

function theChoice(whichMenu) {
switch(whichMenu)
{
case="a":
return "Japanese";
break;

case="b":
return "Mexican";
break;

case="c":
return "Indian";
break;

case="d":
return "American";
break;

default:
return "Not a Choice";
break;
}
}

//-->

</script>

</head>

<body>
<table align="center" width="500" cellspacing="5" cellpadding="5" border="0">
<th align="left">SELECT YOUR MENU CHOICE:</th>
<tr>
<td>a) Japanese</td>
</tr>
<tr>
<td>b) Mexican (legal)</td>
</tr>
<tr>
<td>c) Indian (not Native American)</td>
</tr>
<tr>
<td>d) American </td>
</tr>
</table>

<script language="javascript" type="text/javascript">
<!--
var menuType=prompt("Enter a Letter (a, b, c or d)", "");

var whichMenu=theChoice(menuType);

document.write("<br> The" +whichMenu+ "menu will be brought to your table");

//-->
</script>
</body>
</html>
</code>

Thank You!

John

glenngv
06-19-2007, 06:42 PM
You have a syntax error.

Change this:

case="a":
return "Japanese";
break;
to:

case "a":
return "Japanese";break is unnecessary in this case (though switch normally needs it) as you already do a return which will cause the next statement not to be executed.

jrcoleman
06-19-2007, 08:13 PM
Thank you for your help,

I only needed to tweek the code a bit further, it still gave me a syntax error until I moved the : in front of the case.



case "a":
return "Japanese";

glenngv
06-20-2007, 02:01 AM
Oopps, I didn't realize I did that. :D
Glad you fixed it yourself.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum