SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with if/else statement

    I was wondering if someone could please look at this for me and try to explain what I am doing wrong? When I pull up the error console it tells me there are at least 2 mistakes which are:
    Uncaught SyntaxError: Unexpected token else sureGrade.js:46
    Uncaught ReferenceError: check_grade is not defined gradesPage.html:15.
    Any help would be greatly appreciated. Thank You.

    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>
    function check_grade()
    {
    	 var percent = document.gradeForm.percent.value;
    	 var percent = parseFloat(percent);
    	 var absences = document.gradeForm.absences.value;
    	 var absences = parseInt(absences);
    	
    	If (absences >= 5) 
    		{
    			document.gradeForm.comments.value = "You have an F because of excessive absentees";
    		}
    	  Else If (percent >= 90 || percent <= 100 && absences <= 2)
    		{
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. + \n"
    			"Your Grade: A, Your Percent: + percent * 0.3";
    		}
    	 Else If (percent >= 90 || percent <= 100)
    			document.gradeForm.comments.value = "Your Grade: A, Your Percent: + percent";
    	Else If (percent <= 89.9 || percent  >= 80)
    		{
    			document.gradeForm.comments.value = "Your Grade: B, Your percent: + percent";
    		}
    	Else If  (percent <= 89.9 || percent >= 80 && absences <= 2)
    		{
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. + \n"
    			"Your Grade: B, Your Percent: + percent * 0.3";
    		}
    	Else If (percent <= 79.9 || percent >= 70)
    		{
    			document.gradeForm.comments.value = "Your Grade: C, Your Percent: + percent";
    		}
    	Else If (percent <= 79.9 || percent >= 70 && absences <= 2)
    		{
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. + \n"
    			"Your Grade: C, Your Percent: + percent * 0.3";
    		{
    	Else If (percent <= 69.9 || percent >= 60)
    		{
    			document.gradeForm.comments.value = "Your Grade: D, Your Percent: + percent";
    		}
    	Else If (percent <= 69.9 || percent >= 60 && absences <= 2)
    		{
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. + \n"
    			"Your Grade: D, Your Percent: + percent * 0.3";
    		}
    	Else 
    		{
    			document.gradeForm.comments.value = "I am sorry you have fail this semester. + \n"
    			"Your Grade: F, Your Percent: + percent";
    		}
    		}
    </script>
    </head>
    
    <body>		
    <form name="gradeForm" action="#">
       <label for="absences">How many absences do you have?: </label><br />
       <input type="text" name="absences" /><br />
       <label for="percent">What is your grade percent? </label><br />
       <input type="text" name="percent" /><br />
       <textarea name = "comments" readonly = "true" value = "comments" rows = "5" cols = "20"></textarea>
       <input type="button" value="check_grade" onclick="check_grade()" /><br />
    </form>
    </body>
    </html>

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,213
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Hi plat.

    There's something missing here. There is a reference to a sureGrade.js, which doesn't appear in your code.

  3. #3
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am sorry I have it all in one html page and then I have another in an html and a js page. here is the one with both sheets.

    js

    Code:
    function check_grade()
    {
    	 var percent = document.gradeForm.percent.value;
    	 var percent = parseFloat(percent);
    	 var absences = document.gradeForm.absences.value;
    	 var absences = parseInt(absences);
    	
    		If (absences >= 5) 
    		{
    			document.gradeForm.comments.value = "You have an F because of excessive absentees";
    		}
    	    If (percent >= 90 || percent <= 100 && absences <= 2)
    		{
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. + \n"
    			"Your Grade: A, Your Percent: + percent * 0.3";
    		}
    		If (percent >= 90 || percent <= 100)
    			document.gradeForm.comments.value = "Your Grade: A, Your Percent: + percent";
    		If (percent <= 89.9 || percent  >= 80)
    		{
    			document.gradeForm.comments.value = "Your Grade: B, Your percent: + percent";
    		}
    		If (percent <= 89.9 || percent >= 80 && absences <= 2)
    		{
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. + \n"
    			"Your Grade: B, Your Percent: + percent * 0.3";
    		}
    		If (percent <= 79.9 || percent >= 70)
    		{
    			document.gradeForm.comments.value = "Your Grade: C, Your Percent: + percent";
    		}
    		If (percent <= 79.9 || percent >= 70 && absences <= 2)
    		{
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. + \n"
    			"Your Grade: C, Your Percent: + percent * 0.3";
    		{
    		If (percent <= 69.9 || percent >= 60)
    		{
    			document.gradeForm.comments.value = "Your Grade: D, Your Percent: + percent";
    		}
    		If (percent <= 69.9 || percent >= 60 && absences <= 2)
    		{
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. + \n"
    			"Your Grade: D, Your Percent: + percent * 0.3";
    		}
    		Else 
    		{
    			document.gradeForm.comments.value = "I am sorry you have fail this semester. + \n"
    			"Your Grade: F, Your Percent: + percent";
    		}
    		}
    html

    Code:
    <!doctype html>
    <html>
    	<head>
    		<title>Grade Reporter</title>
    		<meta charset="utf-8" />
    		<script type="text/javascript" src="sureGrade.js"></script>
    	</head>
    <body>		
    <form name="gradeForm" action="#">
       <label for="absences">How many absences do you have?: </label><br />
       <input type="text" name="absences" /><br />
       <label for="percent">What is your grade percent? </label><br />
       <input type="text" name="percent" /><br />
    	 <input type="button" value="Check Grade" id="checkGradeButton"  onclick="check_grade()" />
       <label for="comments">COMMENTS: </label>
       <textarea id="comments"></textarea>
    </form>
    </body>
    </html>
    Thank you for taking the time to look at it.

  4. #4
    SitePoint Evangelist silver trophybronze trophy
    Join Date
    Jul 2013
    Posts
    406
    Mentioned
    22 Post(s)
    Tagged
    0 Thread(s)
    Maybe the error report was when the script was in an external file, unlike the code above.
    Anyway:
    • The <script> has to be: <script type="text/javascript">.
    • All If's and Else If's have to be in lower case: if(...){...} and else if(...){...}.
    • The opening { in the line before Else If (percent <= 69.9 || percent >= 60) has to be a closing }.
    • The calling of the percent variable in the resulting value has to be outside the string of plain text:
      document.gradeForm.comments.value = "Your Grade: B \nYour Percent: " + percent;
    • If you mean that the gain of 3% is added to the given percent, it has to be: + percent * 1.03 (instead of + percent * 0.3).
    • Or: + parseInt(percent * 1.03) in case you want it rounded.
    • Splitted strings over more code lines:
      Code:
      (...).value="..."
                  "..."
      have to be connected by a + sign:
      Code:
      (...).value="..."
                  +"..."

    =======
    Besides, I'm afraid the OR (||) and AND (&&) in the formulas aren't adequate.
    I suppose it has to be something like:
    Code:
    ...
    	if (absences >= 5){
    		document.gradeForm.comments.value = "You have an F because of excessive absentees";
    	}
    	else if (percent>=90 && percent<=100){
    		if (absences <= 2){
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. \n"
    			+"Your Grade: A \nYour Percent: "+ parseInt(percent * 1.03);
    		}
    		else {
    			document.gradeForm.comments.value = "Your Grade: A \nYour Percent: "+ percent;
    		}
    	}
    	else if (percent>=80 && percent<=89.9){
    		if (absences <= 2){
    			document.gradeForm.comments.value = "You have gained 3% to your percent for having few absences. \n"
    			+"Your Grade: B \nYour Percent: "+ parseInt(percent * 1.03);
    		}
    		else {
    			document.gradeForm.comments.value = "Your Grade: B \nYour Percent: "+ percent;
    		}
    	}
    and so on.

  5. #5
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You for taking the time to help me. It all helps plus I had a bracket backwards which was driving me crazy. Thank you.

  6. #6
    SitePoint Enthusiast bronze trophy \\.\'s Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    72
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As I was trying to guide you Plat through your errors on the other forum, you have to first walk before you can run.

    You don't need to understand every term your Javascript Console throws at you, just the line number and that is only a guide as I pointed out, the line number above contains the error which you now know that you had an opening brace rather than a closing brace (curly bracket)


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •