JavaScript GPA计算器无法读取用户输入

时间:2017-09-17 08:07:47

标签: javascript arrays

我是JavaScript的初学者。在这里,我无法弄清楚为什么我的代码不会读取我输入的值,即使我无法找到代码中的任何错误。有任何有用的建议来编辑我的代码来修复错误吗?



function gpacalc() {
  //define valid grades and their values
  var gr = new Array(6);
  var cr = new Array(6);
  var ingr = new Array(5);
  var incr = new Array(5);

  // define valid grades and their values
  var grcount = 11;

  gr[0] = "A+";
  cr[0] = 4;
  gr[1] = "A";
  cr[1] = 4;
  gr[2] = "A-";
  cr[2] = 3.70;
  gr[3] = "B+";
  cr[3] = 3.30;
  gr[4] = "B";
  cr[4] = 3;
  gr[5] = "B-";
  cr[5] = 2.70;
  gr[6] = "C+";
  cr[6] = 2.30;
  gr[7] = "C";
  cr[7] = 2;
  gr[8] = "C-";
  cr[8] = 1.70;
  gr[9] = "D";
  cr[9] = 1;
  gr[10] = "D-";
  cr[10] = 0.70;

  // retrieve user input
  ingr[0] = document.GPACalcForm.GR1.value;
  ingr[1] = document.GPACalcForm.GR2.value;
  ingr[2] = document.GPACalcForm.GR3.value;
  ingr[3] = document.GPACalcForm.GR4.value;
  ingr[4] = document.GPACalcForm.GR5.value;
  incr[0] = document.GPACalcForm.CR1.value;
  incr[1] = document.GPACalcForm.CR2.value;
  incr[2] = document.GPACalcForm.CR3.value;
  incr[3] = document.GPACalcForm.CR4.value;
  incr[4] = document.GPACalcForm.CR5.value;

  // Calculate GPA
  var allgr = 0;
  var allcr = 0;
  var gpa = 0;

  for (var x = 0; x < 5; x++) {
    if (ingr[x] == "")
      break;
    alert("Error: You did not enter a numeric  credits value for subject. If the subject is worth 0 credits, then enter the number 0 in  the field.");

    var validgrcheck = 0;

    for (var xx = 0; xx < grcount; xx++) {
      if (ingr[x] == gr[xx]) {
        allgr = allgr + (parseInt(incr[x], 10) * cr[xx]);
        allcr = allcr + parseInt(incr[x], 10);
        validgrcheck = 1;
        break;
      }
    }

    if (validgrcheck == 0) {
      alert("Error: Could not recognize the grade entered for subject " + eval(x + 1) + ". Please use standard college grades in the form of A+, A, B+ ... D-");
      return 0;
    }
  }

  if (allcr == 0) {
    alert("Error:You did not enter any credit values. GPA = N/A");
    return 0;
  }

  gpa = allgr / allcr;

  alert("GPA =  " + gpa);

  return 0;
}
&#13;
<center>

  <form name="GPACalcForm">

    <table border="2" bgcolor="#bb8fce" cellpadding="5" cellspacing="2">

      <TH> </TH>
      <TH>Grade</TH>
      <TH>Credits</TH>

      <TR>
        <TD>IT 201</TD>
        <TD><input type="text" size="5" name="GR1" align="top" maxlength="5"></TD>
        <TD><input type="text" size="5" name="CR1" align="top" maxlength="5"></TD>
      </TR>

      <TR>
        <TD>IT 202</TD>
        <TD><input type="text" size="5" name="GR2" align="top" maxlength="5"></TD>
        <TD><input type="text" size="5" name="CR2" align="top" maxlength="5"></TD>
      </TR>


      <TR>
        <TD>IT 203</TD>
        <TD><input type="text" size="5" name="GR3" align="top" maxlength="5"></TD>
        <TD><input type="text" size="5" name="CR3" align="top" maxlength="5"></TD>
      </TR>

      <TR>
        <TD>IT 204</TD>
        <TD><input type="text" size="5" name="GR4" align="top" maxlength="5"></TD>
        <TD><input type="text" size="5" name="CR4" align="top" maxlength="5"></TD>
      </TR>

      <TR>
        <TD>IT 205</TD>
        <TD><input type="text" size="5" name="GR5" align="top" maxlength="5"></TD>
        <TD><input type="text" size="5" name="CR5" align="top" maxlength="5"></TD>
      </TR>

      <TR>
        <TR align="center">
          <TD colspan="3">
            <input type="button" value="Calculate" name="CalcButton" OnClick="gpacalc()">
          </TD>
        </TR>

    </table>

  </form>

  <br/>

  <p> </p>

</center>

<br/>
&#13;
&#13;
&#13;

我是JavaScript的初学者。在这里,我无法弄清楚为什么我的代码不会读取我输入的值,即使我可以找出代码中的任何错误。有任何有用的建议来编辑我的代码来修复错误吗?

1 个答案:

答案 0 :(得分:0)

function gpacalc()
	{
		//define valid grades and their values
		var gr = new Array(6); 
		var cr = new Array(6);
		var ingr = new Array(5);
		var incr = new Array(5);

		// define valid grades and their values
		var grcount = 11; 

		gr[0] = "A+";
		cr[0] = 4;
		gr[1] = "A"; 
		cr[1] = 4; 
		gr[2] = "A-";
		cr[2] = 3.70;
		gr[3] = "B+";
		cr[3] = 3.30;
		gr[4] = "B";
		cr[4] = 3;
		gr[5] = "B-";
		cr[5] = 2.70;
		gr[6] = "C+";
		cr[6] = 2.30;
		gr[7] = "C";
		cr[7] = 2;
		gr[8] = "C-";
		cr[8] = 1.70;
		gr[9] = "D";
		cr[9] = 1;
		gr[10] = "D-";
		cr[10] = 0.70;

		// retrieve user input
		ingr[0] = document.GPACalcForm.GR1.value;
    console.log(ingr[0]);
		ingr[1] = document.GPACalcForm.GR2.value;
		ingr[2] = document.GPACalcForm.GR3.value;
		ingr[3] = document.GPACalcForm.GR4.value;
		ingr[4] = document.GPACalcForm.GR5.value;
		incr[0] = document.GPACalcForm.CR1.value;
		incr[1] = document.GPACalcForm.CR2.value;
		incr[2] = document.GPACalcForm.CR3.value;
		incr[3] = document.GPACalcForm.CR4.value;
		incr[4] = document.GPACalcForm.CR5.value;

		// Calculate GPA
		var allgr = 0;
		var allcr = 0;
		var gpa = 0;
		
		for (var x = 0; x < 5; x++)
		{
			if (ingr[x] == "") {
        
			alert("Error: You did not enter a numeric  credits value for subject. If the subject is worth 0 credits, then enter the number 0 in  the field."); 
      break;
      }

        
			var validgrcheck = 0;
		
			for (var xx = 0; xx < grcount; xx++)
			{
				if (ingr[x] == gr[xx])
				{
					allgr = allgr + (parseInt(incr[x],10) * cr[xx]);
					allcr = allcr + parseInt(incr[x],10);
					validgrcheck = 1;
					break;
				}
			}
		 console.log(validgrcheck);
			if (validgrcheck == 0)
			{
                alert("Error: Could not recognize the grade entered for subject " + eval(x +  1) + ". Please use standard college grades in the form of A+, A, B+ ... D-");
                return 0;
			}
		}
		
		if (allcr == 0)
		{
			alert("Error:You did not enter any credit values. GPA = N/A");
			return 0;
		}

		gpa = allgr / allcr;

		alert("GPA =  " + gpa);

		return 0;
	}
<center>

<form name = "GPACalcForm">

<table border = "2" bgcolor = #bb8fce  cellpadding = "5" cellspacing = "2">

<TH> </TH>
<TH>Grade</TH>
<TH>Credits</TH>

<TR>
<TD>IT 201</TD>
<TD><input type = "text" size = "5" name = "GR1" align = "top" maxlength = "5"></TD>
<TD><input type = "text" size = "5" name = "CR1" align = "top" maxlength = "5"></TD>
</TR>

<TR>
<TD>IT 202</TD>
<TD><input type = "text" size = "5" name = "GR2" align = "top" maxlength = "5"></TD>
<TD><input type = "text" size = "5" name = "CR2" align = "top" maxlength = "5"></TD>
</TR>


<TR>
<TD>IT 203</TD>
<TD><input type = "text" size = "5" name = "GR3" align = "top" maxlength = "5"></TD>
<TD><input type = "text" size = "5" name = "CR3" align = "top" maxlength = "5"></TD>
</TR>

<TR>
<TD>IT 204</TD>
<TD><input type = "text" size = "5" name = "GR4" align = "top" maxlength = "5"></TD>
<TD><input type = "text" size = "5" name = "CR4" align = "top" maxlength = "5"></TD>
</TR>

<TR>
<TD>IT 205</TD>
<TD><input type = "text" size = "5" name = "GR5" align = "top" maxlength = "5"></TD>
<TD><input type = "text" size = "5" name = "CR5" align = "top" maxlength = "5"></TD>
</TR>

<TR>
<TR align = "center">
<TD colspan = "3">
<input type = "button" value = "Calculate" name = "CalcButton" OnClick = "gpacalc()">
</TD>
</TR>

</table>

</form>

<br/>

<p> </p>

</center>

<br/>

我在您的代码中进行了以下更改: -

if (ingr[x] == "")
  break;
alert("Error: You did not enter a numeric  credits value for subject. If the subject is worth 0 credits, then enter the number 0 in  the field.");

if (ingr[x] == "") {

        alert("Error: You did not enter a numeric  credits value for subject. If the subject is worth 0 credits, then enter the number 0 in  the field."); 
  break;
  }