2016-04-11 10 views
1

私はフィドルhereを持っています。N/Aをオプションとして計算する

私は2-6回答(回答ができ: 0, 1, 2またはNA、および"1a", "1b", "1c", "2a"と呼ばれている、など)に基づいて平均格付けを計算

    1. にしようとしています。私は スクリプトを使用して評価を正確に平均していますが、すべての回答が "NA"であればいつでも "0" を生成します。すべての回答が "NA"である場合はいつでも、生成平均も となる必要があります。 "NA"です。

      これは私が持っているコードです:

      var aField = new Array("1a", "1b", "1c") //create an array of the fields you need to check the value of 
      
      var aScore = new Array(); //creating an empty array for further use 
      
      var nScore = 0; //creating a variable holding the sum of the scores 
      for (var i = 0; i < aField.length; i++){ //looping through every item of array aField 
      
      if (this.getField(aField[i]).value >= 0){ //if our value is a number eliminating 0, nothing and NA 
      
           aScore.push(aField[i]); //we will count the number of correct values with this array; 
      
           nScore += Number(this.getField(aField[i]).value); // we add the value to our total 
      } 
      } 
      
      if (aScore.length >0){ //We don't want to divide our score by 0 because it's impossible 
      
      event.value = nScore/aScore.length; //we devide the result by the number of correct values in the array. 
      

      誰かが問題を解決するために、次のコードを示唆したが、 が、私はそれを実行したときに、私は

      「にSyntaxError受け取っ:行方不明を、ステートメントの前に、 12: 、行13 "

      メッセージ。 (問題が何であるかはわかりませんが、誰でも がコードを見つけて解決できる場合は、大いに感謝します)。

      var allFieldsAreNA = true; 
      
      
      for (var i = 0; i < aField.length; i++){ 
      if (this.getField(aField[i]).value >= 0) { 
          allFieldsAreNA = false; 
          break; 
      } 
      } 
      
      
      If(allFieldsAreNA == false) { 
      
      // do the code you already have 
      
      } 
      else { 
      event.value = “NA”; // or something like that 
      } 
      
    2. 私は平均格付けを取り、様々な"Weight Factors"(例えば、0.15, 0.1、など)を掛けたスクリプトを取得する必要があります。 avg。 ratingは数値ですが、avgの場合は単純に乗算されます。評価が"NA"の場合、"Weighted Rating""NA"です。

    3. "Weighted Ratings"を合計し、合計を"Weight Factors"で割るスクリプト。すべてのフィールドが数値であると計算される場合、総重み係数は1.0または100%.1, .15, .15, .1, .15, .25, .1)であり、1.0で割った総"Weighted Ratings"に等しい... NAある加重評価がある場合、総"Weight Factors"は加重(それに応じて計算されるべきですカテゴリ6 is NAの評価、したがってWeight Factors.1 + .15 +.15 + .1 + .15 + .1である必要があります)、合計"Weighted Ratings"/0.75です。

    ご協力いただきありがとうございます。

  • 答えて

    0

    OPのコードの2番目のバッチは、すべてのフィールドに値として "NA"が含まれているかどうかを識別し、に変数を設定する必要がある場合はallFieldsAreNAに設定します。コード内の問題はこの行にあるようですIf(allFieldsAreNA == false) {ifステートメントは大文字で始まり、小文字のiで始まる必要があります。

    ここから、実行する必要のある計算に「NA」を印刷するのは簡単です。

    OP問題の第3の部分はちょっと混乱します。 allFieldsAreNA変数に値が設定されても、計算するのはかなり簡単です。

    私はしばらく前に答えたPDFのJSに関する同様の質問があります。そこのコードは参考にもなります。フォーム内で同様の計算を行います。Count checkboxes in a form