2017-09-17 11 views
1

私はJavaScriptの初心者です。ここでは、自分のコードが私が入力した値を読み込んでいない理由を理解することはできません。でも、私のコードではエラーを見つけることはできません。エラーを修正するために私のコードを編集するための助言があれば教えてください。JavaScript GPA計算機がユーザー入力を読み取っていません

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; 
 
}
<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/>

私はJavaScriptに初心者です。ここで私は自分のコードが私が入力した値を読み込んでいない理由を理解できません。エラーを修正するために私のコードを編集するための助言があれば教えてください。

答えて

0

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

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

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

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

 
\t \t // Calculate GPA 
 
\t \t var allgr = 0; 
 
\t \t var allcr = 0; 
 
\t \t var gpa = 0; 
 
\t \t 
 
\t \t for (var x = 0; x < 5; x++) 
 
\t \t { 
 
\t \t \t if (ingr[x] == "") { 
 
     
 
\t \t \t 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; 
 
     } 
 

 
     
 
\t \t \t var validgrcheck = 0; 
 
\t \t 
 
\t \t \t for (var xx = 0; xx < grcount; xx++) 
 
\t \t \t { 
 
\t \t \t \t if (ingr[x] == gr[xx]) 
 
\t \t \t \t { 
 
\t \t \t \t \t allgr = allgr + (parseInt(incr[x],10) * cr[xx]); 
 
\t \t \t \t \t allcr = allcr + parseInt(incr[x],10); 
 
\t \t \t \t \t validgrcheck = 1; 
 
\t \t \t \t \t break; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t console.log(validgrcheck); 
 
\t \t \t if (validgrcheck == 0) 
 
\t \t \t { 
 
       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; 
 
\t \t \t } 
 
\t \t } 
 
\t \t 
 
\t \t if (allcr == 0) 
 
\t \t { 
 
\t \t \t alert("Error:You did not enter any credit values. GPA = N/A"); 
 
\t \t \t return 0; 
 
\t \t } 
 

 
\t \t gpa = allgr/allcr; 
 

 
\t \t alert("GPA = " + gpa); 
 

 
\t \t return 0; 
 
\t }
<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; 
    } 
+0

以前のあなたのコードに構文エラーがあります。私はそれを修正しました。 – Lalit

+0

OMG!私はそんなに狂ってる!エラーを指摘するために@Lalitありがとうございます!私のコードが修正されました!再度、感謝します! :) –

関連する問題