7 Replies - 1637 Views - Last Post: 17 January 2010 - 01:41 PM

#1 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Help me with my If Else

Posted 16 January 2010 - 09:28 PM

We're asked to Create a program that will accept input from prelim grade, midterm grade and final grade and calculate it. The formula of the calculation for the semestral grade will be sg=((pg*.30)+(mg*.30)+(fg*.40)). If the input is <=65 and >100, a prompt will appear and say "INVALID ENTRY" and request to input again.

If the input is ='0', a prompt will appear and say "NO GRADE", If the input is ='20', a prompt will appear and say "Not Attending",If the input is ='30', a prompt will appear and say "Dropped", If the input is ='40', a prompt will appear and say "Incomplete",It will show what you've input and continue to input midterm grade. Same with the midterm and finals.

I got here my program for Prelim Grade but I got problem with the <=65 and >100. If I input 65, it will prompt and say INVALID ENTRY.After I input an Invalid Entry again, nothing will happen. After I input another invalid entry, a prompt will appear but after I input a valid entry, the valid entry won't show. It must be not stopping to prompt INVALID ENTRY and Request to input again until you Input a valid entry. After you input a valid entry, it will be shown on the browser.

we're asked to use do..while and while

<html>
	<head>
		<title>Web Programming Laboratory Exam</title>
	</head>
<body>
	<script language="Javascript">

	

do
{
	{
	var pg=prompt('Enter Prelim Grade','0');
	
	if(pg==0)
		{
		alert('No Grade');
		document.write('<br>Prelim Grade is '+pg);
		}
	else if(pg==20)
		{
		alert('Not Attending');
		document.write('<br>Prelim Grade is '+pg);
		}
	else if(pg==30)
		{
		alert('Dropped');
		document.write('<br>Prelim Grade is '+pg);
		}
	else if(pg==40)
		{
		alert('Incomplete');
		document.write('<br>Prelim Grade is '+pg);
		}

	else if (pg<=65)
		{
		alert('Invalid Entry');
		var pg=prompt('Enter Prelim Grade','0');
		}
	else if (pg>100)
		{
		alert('<br>Invalid Entry');
		var pg=prompt('Enter Prelim Grade','0');
		}
	else
		document.write('Prelim Grade is '+pg);
		ppg=pg*.30
	}

}

while (pg<=65)
	{
	
	}

	</script>
</body>
</html>



I'm asking how my if..else on <=65 and >100 won't stop to ask me to input a valid entry and after I input a valid entry, the valid entry must be shown and continue to Midterm Grade.

Is This A Good Question/Topic? 0
  • +

Replies To: Help me with my If Else

#2 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,448
  • Joined: 10-May 07

Re: Help me with my If Else

Posted 16 January 2010 - 09:30 PM

What is the purpose of nested brackets under the do loop?

Quote

do
{
	{



One pet peve that I have is if/else if/else if/ else if when the programmer is checking the same value each time.

Since this is an int value, why not run a select case on it?

or just check with multiple ifs? Why drag it out when your 1st check has failed or passed?

	if(pg==0)
		{
		alert('No Grade');
		document.write('<br>Prelim Grade is '+pg);
		}
	if(pg==20)
		{
		alert('Not Attending');
		document.write('<br>Prelim Grade is '+pg);
		}
   if(pg==30)
// ... and so on



switch(pg)
{
case 0:
  alert('No Grade');
  break;
case 20:
  alert('Not Attending');
  break;
case 30:
  alert('Dropped');
  break
case 40:
  alert('Incomplete');
  break
default:
  alert('Error...');
  break;
}


& so on...
Was This Post Helpful? 0
  • +
  • -

#3 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Re: Help me with my If Else

Posted 16 January 2010 - 10:21 PM

View Postno2pencil, on 16 Jan, 2010 - 08:30 PM, said:

What is the purpose of nested brackets under the do loop?

Quote

do
{
	{



One pet peve that I have is if/else if/else if/ else if when the programmer is checking the same value each time.

Since this is an int value, why not run a select case on it?

or just check with multiple ifs? Why drag it out when your 1st check has failed or passed?

	if(pg==0)
		{
		alert('No Grade');
		document.write('<br>Prelim Grade is '+pg);
		}
	if(pg==20)
		{
		alert('Not Attending');
		document.write('<br>Prelim Grade is '+pg);
		}
   if(pg==30)
// ... and so on



switch(pg)
{
case 0:
  alert('No Grade');
  break;
case 20:
  alert('Not Attending');
  break;
case 30:
  alert('Dropped');
  break
case 40:
  alert('Incomplete');
  break
default:
  alert('Error...');
  break;
}


& so on...


I tried your code but everything I input is "Error" even I input 0,20,30 or 40. I put your code inside the do...while loop.


I changed this line
default:
  alert('Error...');
  break;


to this

default:
	document.write('Prelim Grade is '+pg);
  break;

So it won't prompt ERROR again.
This is my new program

<html>
	<head>
		<title>Web Programming Laboratory Exam</title>
	</head>
<body>
	<script language="Javascript">

do
		{
		var pg=prompt('Enter Prelim Grade','0');	
	   switch(pg)
				  {
				  case 0:
				  alert('No Grade');
				  break;
				  case 20:
				  alert('Not Attending');
				  break;
				  case 30:
				  alert('Dropped');
				  break;
				  case 40:
				  alert('Incomplete');
				  break;
				  default:
			  document.write('Prelim Grade is '+pg);
				  break;
				  }
	if (pg<=65)
		{
		alert('Invalid Entry');
		var pg=prompt('Enter Prelim Grade');
		}
	else if (pg>100)
		{
		alert('Invalid Entry');
		var pg=prompt('Enter Prelim Grade');
		}

}
while (pg<=65)
	{
	
	}

	</script>
</body>
</html>

The problem here now is when I put an Invalid Entry or 0,20,30, or 40, the Prompt will say Invalid Entry. How can I run this properly? If I input 0,20,30 or 40, it must not prompt Invalid Entry. Another problem here is if I input an invalid Entry, the entry that I entered will be shown. it must not. If I input again an invalid entry, nothing will happen, it will just ask me to input again. If I input a valid entry, none is shown. I'm getting confused. I think switch statement is not com applicable for this program

This post has been edited by k3nnt0ter0: 16 January 2010 - 10:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,448
  • Joined: 10-May 07

Re: Help me with my If Else

Posted 16 January 2010 - 10:34 PM

The following works for me :

<html>
	<head>
		<title>Web Programming Laboratory Exam</title>
	<script language="Javascript">
	function pg_func() {
			var pg;
			pg=prompt('Enter Prelim Grade','');
			switch(pg) {
				case "0":
					alert('No Grade');
					break;
				case "20":
					alert('Not Attending');
					break;
				case "30":
					alert('Dropped');
					break;
				case "40":
					alert('Incomplete');
					break;
				default:
				break;
			}
			return pg;
	}
	</script>
	</head>
<body>
	<script language="Javascript">
	var pg_test;
	pg_test=pg_func();
	document.write('Prelim Grade is '+pg_test);
	</script>
</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#5 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Re: Help me with my If Else

Posted 16 January 2010 - 10:45 PM

View Postno2pencil, on 16 Jan, 2010 - 09:34 PM, said:

The following works for me :

<html>
	<head>
		<title>Web Programming Laboratory Exam</title>
	<script language="Javascript">
	function pg_func() {
			var pg;
			pg=prompt('Enter Prelim Grade','');
			switch(pg) {
				case "0":
					alert('No Grade');
					break;
				case "20":
					alert('Not Attending');
					break;
				case "30":
					alert('Dropped');
					break;
				case "40":
					alert('Incomplete');
					break;
				default:
				break;
			}
			return pg;
	}
	</script>
	</head>
<body>
	<script language="Javascript">
	var pg_test;
	pg_test=pg_func();
	document.write('Prelim Grade is '+pg_test);
	</script>
</body>
</html>

It works on me too but we're getting far with the instruction to use do..while and while.
Let's go back to the problem on my first post.

Quote

I'm asking how my if..else on <=65 and >100 won't stop to ask me to input a valid entry and after I input a valid entry, the valid entry must be shown and continue to Midterm Grade.

This post has been edited by k3nnt0ter0: 16 January 2010 - 10:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,448
  • Joined: 10-May 07

Re: Help me with my If Else

Posted 16 January 2010 - 11:07 PM

Using the switch case, I would set a variable & adjust it accordingly.

If I understand you correctly, you want the prompt to continue to ask if the value is less than 65 or greater than 100, but only ask once & report if the value is inbetween?
Was This Post Helpful? 0
  • +
  • -

#7 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Re: Help me with my If Else

Posted 16 January 2010 - 11:53 PM

Ok. This is my code from prelim to finals. The missing part here is when the input is less than or equal to 65 and greater than 100, the prompt will say INVALID ENTRY. However, after it says Invalid Entry, it must ask you to enter again an Entry until you input a valid entry. It must be terminated after you input either 0,20,30,40 or after you input a valid entry and after the program calculated it. This program does not terminate after I input valid entries. It just continue to ask grade again and again. I don't know how will I put those code inside the program. I'm getting really confused.
<html>
	<head>
		<title>Web Programming Laboratory Exam</title>
	</head>
<body>
	<script language="Javascript">
do
{
	{
	var pg=prompt('Enter Prelim Grade','0');
	if(pg==0)
		{
		alert('No Grade');
		document.write('<br>Prelim Grade is '+pg);
		}
	else if(pg==20)
		{
		alert('Not Attending');
		document.write('<br>Prelim Grade is '+pg);
		}
	else if(pg==30)
		{
		alert('Dropped');
		document.write('<br>Prelim Grade is '+pg);
		}
	else if(pg==40)
		{
		alert('Incomplete');
		document.write('<br>Prelim Grade is '+pg);
		}
	else
		document.write('Prelim Grade is '+pg);
	var mg=prompt('Enter Midterm Grade','0');
	}
	{
	if(mg==0)
		{
		alert('No Grade');
		document.write('<br>Midterm Grade is '+mg);
		}
	else if(mg==20)
		{
		alert('Not Attending');
		document.write('<br>Midterm Grade is '+mg);
		}
	else if(mg==30)
		{
		alert('Dropped');
		document.write('<br>Midterm Grade is '+mg);
		}
	else if(mg==40)
		{
		alert('Incomplete');
		document.write('<br>Midterm Grade is '+mg);
		}
	else
		document.write('</br>Midterm Grade is '+mg);
	var fg=prompt('Enter Final Grade','0');
	}
	{
	if(fg==0)
		{
		alert('No Grade');
		document.write('<br>Final Grade is '+fg);
		}
	else if(fg==20)
		{
		alert('Not Attending');
		document.write('<br>Final Grade is '+fg);
		}
	else if(fg==30)
		{
		alert('Dropped');
		document.write('<br>Final Grade is '+fg);
		}
	else if(fg==40)
		{
		alert('Incomplete');
		document.write('<br>Final Grade is '+fg);
		}
	else
		{
		document.write('</br>Final Grade is '+fg);
		sg=((pg*.3)+(mg*.3)+(fg*.4));
		document.write('<br>Your Semestral Grade is '+sg);
		}
	}
	if (sg>=74.5)
			{
			document.write('<br>Remarks: Passed');
			document.write('</br>This Program is developed by -k3nN');
			}
	else
			 if (sg<74.5)
			{
			document.write('<br>Remarks:Failed');
			document.write('</br>This Program is developed by -k3nN');
			}
}
while (sg>=74.5|sg<74.5)
	{
	document.write('</br>Thanks')
	}
	</script>
</body>
</html>

This post has been edited by k3nnt0ter0: 17 January 2010 - 01:55 AM

Was This Post Helpful? 0
  • +
  • -

#8 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Help me with my If Else

Posted 17 January 2010 - 01:41 PM

You could try using this code:


<html>
	<head>
		<title>Web Programming Laboratory Exam</title>
		<script type="text/javascript">
			var valid = 0;
			var pg , mg , fg;
			//This is the function that determines if the preliminary grade is valid:
			function prelimGrade(){
				//A while statement to continue requesting a grade until a valid grade is input
				do
				{
					pg = prompt("Enter Prelim Grade",'0');
					/*This checks if the grade is less than or equal to 65 or greater than 100,
					it also checks to see if it is equal to 0, 20, 30, or 40 when it is less than 65*/
					if (pg <= 65 && pg != 0 && pg != 20 && pg != 30 && pg != 40 || pg > 100)
					{
						alert("INVALID ENTRY")
					}else{
						if (pg <= 100 && pg > 65) { valid = 1; }
						else{
							switch(pg){
								case "0":
									alert("No Grade");
									valid = 1;
									break;
								case "20":
									alert("Not Attending");
									valid = 1;
									break;
								case "30":
									alert("Dropped");
									valid = 1;
									break;
								case "40":
									alert("Incomplete");
									valid = 1;
									break;
								default:
									break;
							}
						}
					}
				}
				while (valid == 0);
				return pg;
			}
			//This is the function that determines if the midterm grade is valid:
			function midGrade(){
				do
				{
					mg = prompt("Enter Midterm Grade",'0');
					/*This checks if the grade is less than or equal to 65 or greater than 100,
					it also checks to see if it is equal to 0, 20, 30, or 40 when it is less than 65*/
					if (mg <= 65 && mg != 0 && mg != 20 && mg != 30 && mg != 40 || mg > 100)
					{
						alert("INVALID ENTRY")
					}else{
					if (mg <= 100 && mg > 65) { valid = 2; }
						else{
							switch(mg){
								case "0":
									alert("No Grade");
									valid = 2;
									break;
								case "20":
									alert("Not Attending");
									valid = 2;
									break;
								case "30":
									alert("Dropped");
									valid = 2;
									break;
								case "40":
									alert("Incomplete");
									valid = 2;
									break;
								default:
									break;
							}
						}
					}
				}
				while (valid == 1);
				return mg;
			}
			//This is the function that determines if the final grade is valid:
			function finalGrade(){
				do
				{
					fg = prompt("Enter Final Grade",'0');
					/*This checks if the grade is less than or equal to 65 or greater than 100,
					it also checks to see if it is equal to 0, 20, 30, or 40 when it is less than 65*/
					if (fg <= 65 && fg != 0 && fg != 20 && fg != 30 && fg != 40 || fg > 100)
					{
						alert("INVALID ENTRY")
					}else{
						if (fg <= 100 && fg > 65) { valid = 3; }
						else{
							switch(fg){
								case "0":
									alert("No Grade");
									valid = 3;
									break;
								case "20":
									alert("Not Attending");
									valid = 3;
									break;
								case "30":
									alert("Dropped");
									valid = 3;
									break;
								case "40":
									alert("Incomplete");
									valid = 3;
									break;
								default:
									break;
							}
						}
					}
				}
				while (valid == 2);
				return fg;
			}
		</script>
	</head>
	<body>
		<script>
			var pre , mid , fin , sem;
			//This sets the preliminary grade:
			pre = prelimGrade();
			//This sets the midterm grade:
			mid = midGrade();
			//This sets the final grade:
			fin = finalGrade();
			//This sets the semester grade:
			sem = ((pre*.3) + (mid*.3) + (fin*.4));
			//This writes the grades to the webpage:
			document.write("<br>Prelim Grade is " + pre + "<br>Midterm Grade is " + mid + "<br>Final Grade is " + 

fin + "<br>Semester Grade is " + sem);
		</script>
	</body>
</html>



It does exactly what you are describing and uses do...while statements.

This post has been edited by psyking: 17 January 2010 - 02:14 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1