2

私はチャーター スクールで働いており、javascript の使い方を学んでいます。以前私の役職に就いていた人が書いたコードがありますが、うまくいくように思えますが、うまくいきません。

これは、SIS のカスタム HTML ページにあるものです。

GED Status: <script language="Javascript">gedCheck('~(ELC_tspp_GED_read_score)','~ (ELC_tspp_GED_wri_score)','~(ELC_tspp_math_GED_score)','~(ELC_science_state_exam_score)','~(soc_sci_state_exam_score)')</script>

JavaScriptルーチンが各値を評価して少なくとも410であることを確認しているため、さまざまなDBフィールドから値を正しく取得しているようです。

JavaScript ルーチン コードは次のとおりです。

function gedCheck(read,wri,math,sci,soc) {

if( read < 0 && read > 1000 )
    read = 0;
if( wri < 0 && wri > 1000 )
    wri = 0;
if( math < 0 && math > 1000 )
    math = 0;
if( sci < 0 && read > 1000 )
    read = 0;
if( soc < 0 && soc > 1000 )
    soc = 0;        

if ( (read >= 410) && (wri >= 410) && (math >= 410) && (sci >= 410) && (soc >= 410) ) {
    if( read+wri+math+sci+soc >= 2250 )
        document.write( "PASSED" )
}
else
    document.write( "NOT PASSED" )
}

GED テストのすべてのスコアが少なくとも 410 であること、およびすべてのスコアの合計が少なくとも 2250 であることを確認することになっています。ただし、最後の部分までは進んでいません。すべてのスコアが 410 を超えると、「PASSED」が返されます。

私はこれを試しましたが、これもうまくいきません。

function gedCheck(read,wri,math,sci,soc) {

if( read < 0 && read > 1000 )
    read = 0;
if( wri < 0 && wri > 1000 )
    wri = 0;
if( math < 0 && math > 1000 )
    math = 0;
if( sci < 0 && read > 1000 )
    read = 0;
if( soc < 0 && soc > 1000 )
    soc = 0;        

if ( (read >= 410) && (wri >= 410) && (math >= 410) && (sci >= 410) && (soc >= 410) )   {
    if( read+wri+math+sci+soc/5 >= 450 )
        document.write( "PASSED" )
}
else
    document.write( "NOT PASSED" )
}

誰かがこれを解決するのを手伝ってくれますか?

4

4 に答える 4

0
function gedCheck(read, wri, math, sci, soc) {

    if( read < 0 || read > 1000 )
        read = 0;

    if( wri < 0 || wri > 1000 )
        wri = 0;

    if( math < 0 && math > 1000 )
        math = 0;

    if( sci < 0 && read > 1000 )
        read = 0;

    if( soc < 0 && soc > 1000 )
        soc = 0;

    var total = read + wri + math + sci + soc;

    if (read >= 410 && wri >= 410 && math >= 410 && sci >= 410 && soc >= 410 && total >= 2250)   {   
        document.write("PASSED");
    } else {
        document.write("NOT PASSED");
    }
}

その最初のセクション全体は不可能なコードでした。数値がゼロ未満と1000より大きいの両方であるかどうかを確認していました。明らかに不可能なので、ORを使用するように変更しました。

また、他のすべてと同じ方法で確認できる合計変数も作成しました。

于 2013-02-19T19:28:13.860 に答える
0

どうですか

if ((read >= 410) && 
    (wri  >= 410) && 
    (math >= 410) && 
    (sci  >= 410) && 
    (soc  >= 410) &&
    (read+wri+math+sci+soc >= 2250)) {    

    document.write( "PASSED" )
} else {

    document.write( "NOT PASSED" )
}
于 2013-02-19T19:24:20.583 に答える
0

To get the average, you'll want to do this:

(((read + wri + math + sci + soc) / 5) > 450)

The parenthesis around the addition ensures that you divide the sum of all scores by 5. The way that you have it now, you are only dividing the soc score by 5.

Edit (Rewriting the entire method):

function gedCheck(read, wri, math, sci, soc) {
// As was said before, these should all be ORs
// If the score is less than 0, OR greater than 1000
if( read < 0 || read > 1000 ) {
    read = 0;
}
if( wri < 0 || wri > 1000 ) { // I prefer to put the braces around all if/else statements just for absolute clarity
    wri = 0;
}
if( math < 0 || math > 1000 ) {
    math = 0;
}
if( sci < 0 || read > 1000 ) {
    read = 0;
}
if( soc < 0 || soc > 1000 ) {
    soc = 0;        
}

if ( read >= 410 && // Doing this will only pass the student
     wri >= 410  && // if ALL of the conditions are met.
     math >= 410 && 
     sci >= 410  && 
     soc >= 410  &&
     (    (read + wri + math + sci + soc) >= 2250 || // Separated more for clarity
          ((read + wri + math + sci + soc) / 5) > 450) ) { 
    // Either all scores total over 2250
    // Or the average of all 5 are over 450 to pass                
        document.write( "PASSED" )
}
else
    document.write( "NOT PASSED" )
}
于 2013-02-19T19:26:51.520 に答える
0

ここで配列を使用すると、重複するコードの量を減らすのに役立ちます

function gedCheck(read, wri, math, sci, soc) {
   var subjects, totalScore, averageScore;

   subjects = [read, wri, math, sci, soc];
   totalScore = 0;
   averageScore = 0;

   for (var i = 0; i < subjects.length; i++) {
       if (subjects[i] < 0 || subjects[i] > 1000) {
           subjects[i] = 0;
       }
       totalScore += subjects[i];
   };

   averageScore = totalScore / subjects.length;

   if (averageScore >= 450 || totalScore >= 2250) {
       document.write("PASSED");
   } else {
       document.write("NOT PASSED");
   }
}

最初のループは、各科目を繰り返し処理し、必要に応じてゼロに設定してから、合計スコア変数に追加します。

次に、合計スコアを被験者数で平均します。

その後、平均スコアが 450 以上、または 2250 以上の場合、合格となります。

于 2013-02-19T19:40:10.297 に答える