Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Will someone please help me with this code. : ) 1

Status
Not open for further replies.

lmarshall

MIS
Jun 11, 2002
108
US
Will someone please help me with this code. The user is suppose to type in a sport in the textbox and it is suppose to alert the user where they play the sport. I would greatly appreciate it!!!

Thanks

Lmarshall


<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Sports.html</title>
<SCRIPT LANGUAGE= &quot;JavaScript1.2&quot;>
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function sport_Name(sport) {
var sport= &quot;&quot;;
switch (sport) {
case &quot;golf&quot;:
alert(&quot;Golf is played on a golf course.&quot;);
break;
case &quot;tennis&quot;:
alert (&quot;Tennis is played on a tennis court.&quot;);
break;
case &quot;baseball&quot;:
alert(&quot;Baseball is played on a baseball diamond.&quot;);
break;
case &quot;basketball&quot;:
alert (&quot;Basketball is played on a basketball court.&quot;);
break;
}
}
</SCRIPT>
</head>

<body>
<FORM NAME=&quot;TYPES&quot;>
What sport do you want to look up? <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;sport&quot; SIZE=&quot;25&quot;> <BR>
<INPUT TYPE=&quot;button&quot; VALUE=&quot;Look up what they play on&quot; onClick=&quot;sport_Name();&quot;>
</FORM>
</body>
</html>
 
two things pop out:
1: onClick=&quot;sport_Name(); you are not sending the function a value - you should have something in the () if you are going to use function sport_Name(sport)
2: even if you do send it- you are resetting it to &quot;&quot; with var sport= &quot;&quot;; this does not need to be there at all.... since sport is stated in function sport_Name(sport) it is already an acceptable variable ready for use in your function
 
I believe also that the switch statement only works with an integer in the (). At least this is how it works in Java.

You may need to write a nested if/else
 
you've got some minor erros in there. here's how I would do it. you were also missing the ending -->
you should deffinetely use the case function to change the input to lower case also. heres the code I ran that worked ok.
hope that helps
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Sports.html</title>
<SCRIPT LANGUAGE= &quot;JavaScript1.2&quot;>
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function sport_Name() {
str = TYPES.sport.value
str = str.toLowerCase();
switch (str) {
case &quot;golf&quot;:
alert(&quot;Golf is played on a golf course.&quot;);
break;
case &quot;tennis&quot;:
alert(&quot;Tennis is played on a tennis court.&quot;);
break;
case &quot;baseball&quot;:
alert(&quot;Baseball is played on a baseball diamond.&quot;);
break;
case &quot;basketball&quot;:
alert(&quot;Basketball is played on a basketball court.&quot;);
break;
}
}
-->
</SCRIPT>
</head>

<body>
<FORM NAME=&quot;TYPES&quot;>
What sport do you want to look up? <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;sport&quot; SIZE=&quot;25&quot;> <BR>
<INPUT TYPE=&quot;button&quot; VALUE=&quot;Look up what they play on&quot; onClick=&quot;sport_Name();&quot;>
</FORM>
</body>
</html> A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top