【问题标题】:Calculating a rating with N/A as an option以 N/A 选项计算评级
【发布时间】:2016-04-11 15:18:05
【问题描述】:

我有一个小提琴here

我正在尝试

  1. 根据 2-6 个回答计算平均评分(答案可以是: 0, 1, 2NA,并被称为 "1a", "1b", "1c", "2a" 等)。我有一个 正确平均收视率的脚本,但产生"0" 每当所有答案都是"NA"。我还需要产生的平均值 只要所有答案都是"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.
    

    有人建议使用以下代码来解决问题,但是当 我运行它,我收到了一个

    "语法错误:缺失;在语句 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 等)。如果平均。评级是一个数字,然后它只是乘以,如果平均。评级是"NA",那么"Weighted Rating" 也是"NA"

  3. 将所有"Weighted Ratings" 相加并除以"Weight Factors" 的总和的脚本。如果所有字段都计算为数字,则总权重因子为 1.0100% (.1, .15, .15, .1, .15, .25, .1),总 "Weighted Ratings" 除以 1.0 等于... 987654343@,则应相应计算总"Weight Factors"(类别6 is NA的加权评级,因此Weight Factors应仅为.1 + .15 +.15 + .1 + .15 + .1),总"Weighted Ratings" / 0.75

任何帮助,不胜感激。

【问题讨论】:

    标签: javascript adobe acrobat


    【解决方案1】:

    OP 中的第二批代码应确定是否所有字段都具有“NA”作为值,如果是这种情况,请将allFieldsAreNA 变量设置为true。看起来代码中的问题出在If( allFieldsAreNA == false ) { 这一行。 if 语句大写,应以小写i 开头。

    从这里打印出“NA”用于需要完成的计算应该相当容易。

    OP 问题的第三部分有点令人困惑。不过,一旦填充了 allFieldsAreNA 变量,计算起来似乎很容易。

    前段时间我回答了一个关于 PDF 中 JS 的类似问题。那里的代码也可以用作参考。它以表格形式进行类似的计算。 Count checkboxes in a form

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 2015-04-28
      相关资源
      最近更新 更多