2 Replies - 2273 Views - Last Post: 17 January 2010 - 09:12 PM

#1 cod4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-January 10

Can't get Switch Statement to Work.

Posted 17 January 2010 - 04:57 PM

I am having trouble with a switch statement Assignment. I don't really know if what I am doing is even close to right.

Assignment:

Switch

Create a HTML that uses Javascript. Create a page that tells a student the range their mark falls in if they know their letter grade. Below 50 is an "F", 50-59.9 is a D, 60 - 69.9 is a "C", 70 - 79.9 is a "B", 80 - 89.9 is an "A" and 90 - 100 is an "A+". You determine the input and output format.

<html>
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
	<head>
		<title>Switch</title>
		
	<script type="text/javascript"> 
	
	
	var Mark = document.getElementById('letter').value
	
	switch(Mark)
	{
	
	case 1:
	if (Mark = A+ || Mark = a+)
	alert("Your Mark Range is 90% - 100%");
	break;
	
	case 2:
	if (Mark = A || Mark = a)
	alert("Your Mark Range is 80% - 89.9%");
	break;
	
	case 3:
	if (Mark = B || Mark = b)
	alert("Your Mark Range is 70% - 79.9%");
	break;
	
	case 4:
	if (Mark = C || Mark = c)
	alert("Your Mark Range is 60% - 69.9%");
	break;
	
	case 5:
	if (Mark = D || Mark = d)
	alert("Your Mark Range is 50-59.9");
	break;
	
	case 6:
	if (Mark = F || Mark = f)
	alert("Your Mark Range is below 50%");
	break;
	
	
	}
	</script> 
	</head>
	
	
	<body>
	
	<p/>
	<center><b><u>Please Enter Letter Grade to find Mark Range.</u></b></center>
	<p/>
	<form name="my_form" action="" method="get">
	<center>
	
	
	<input type="text" name="Mark1" id="letter" value="">
	</center>
	
	
	<p/>
	
	<center>
	<input type="button" name="button1" value="Submit" onclick="switch()"> 
	</center>
	
	</form>  


	</body>
	
</html>

This post has been edited by cod4ever: 17 January 2010 - 04:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't get Switch Statement to Work.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Can't get Switch Statement to Work.

Posted 17 January 2010 - 05:55 PM

Ok, we have multiple problems here so lets go through them one by one...

1) You can't get the element 'Mark' first thing when the script runs because this javascript is going to run before the textbox 'Mark' is created. To prevent this from being a problem, wrap your code up into a function which we can call at a later date (like when the button is clicked) and at that time the textbox will have been created.

2) Remember when you use a switch statement that the cases are the values that Mark will have that you want to test for. So case 1 means that when Mark equals 1, do that code. Obviously the user is going to be entering in letter grades, so case 1, case 2 etc is not what you want. You want actual letter grades as the cases. case "A+" or case "B"

3) When you test for equality remember that it is two double equal signs, not a single one. Mark = "A+" is not right, you want Mark == "A+" instead. Single equal sign is the assignment operator and would always be true.

4) Related to number 3, you are testing the value against a string value. Remember string values must appear in double quotes. So not Mark == A+ it is Mark == "A+". But this is a mute point since your if statements are no longer needed since the switch case tests cases.

5) To help for test both "B" and "b" we are going to uppercase the value in the text box and then test the upper case version only. This will cut out all the second tests we would need.

6) You can't call your function "switch()" because that is reserved strictly for the switch case. So the function we wrap the code up in will be a different name and that name is what we call from the button.

7) On your switch you will want to account for when the user types an invalid amount. So we can add a default case which will catch any values that were not specified in the other cases. That is a perfect spot to tell the user their entry is invalid.

So your code may look something like this...


<html>
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
	<head>
		<title>Switch</title>
	   
	<script type="text/javascript">
   
	function getScore() {
		var Mark = document.getElementById('letter').value.toUpperCase();
	   
		switch(Mark)
		{
	   
			case "A+":
			alert("Your Mark Range is 90% - 100%");
			break;
		   
			case "A":
			alert("Your Mark Range is 80% - 89.9%");
			break;
		   
			case "B":
			alert("Your Mark Range is 70% - 79.9%");
			break;
		   
			case "C":
			alert("Your Mark Range is 60% - 69.9%");
			break;
		   
			case "D":
			alert("Your Mark Range is 50-59.9");
			break;
		   
			case "F":
			alert("Your Mark Range is below 50%");
			break;

			default:
			alert("Not a valid mark, please try again!");
	   
	   
		}
	}
	</script>
	</head>
   
   
	<body>
   
	<p/>
	<center><b><u>Please Enter Letter Grade to find Mark Range.</u></b></center>
	<p/>
	<form name="my_form" action="" method="get">
	<center>
   
   
	<input type="text" name="Mark1" id="letter" value="">
	</center>
   
   
	<p/>
   
	<center>
	<input type="button" name="button1" value="Submit" onclick="getScore()">
	</center>
   
	</form>  


	</body>
   
</html>



Here our button is calling our new function getScore() which wraps up all the code. When this function is called the Mark textbox will have been created so we can fetch the value, upper case it (using the toUpperCase() method) and then we go through the switch testing each case value. If they enter a valid mark it will pop up the appropriate alert statement. If they enter an invalid mark it will fall into the default case and report an error.

Play around with this to see how you can add more cases. How would you get it to print the same alert message for marks "A+" and "A" WITHOUT repeating the alert line code? Hint, check out how break in a switch statement works.

:)
Was This Post Helpful? 0
  • +
  • -

#3 cod4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-January 10

Re: Can't get Switch Statement to Work.

Posted 17 January 2010 - 09:12 PM

Thanks that Was a lot of help. I really didn't know what direction I was going in.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1