繁体   English   中英

Google脚本:统计投票功能

[英]Google Scripts: Function to tally votes

我正在尝试创建一个简单的Google电子表格功能,以将等级转换为分数系统,然后添加分数。 有权访问电子表格的人(A,B,C,D)将根据第一选择(1),第二选择(2)和第三选择(3)对他们的选择(W,X,Y,Z)进行排名基础。

行是选民,选择是列。

每1票= 3分; 2票= 2分; 3票= 1分。

由于某种原因,我的脚本仅输出脚本的长度,而不输出分数的总和。 您能帮我确定我的脚本出了什么问题吗?

function tally(rankarray) {
  var rank = 0;       // this is the vote cast
  var result = 0;     // this will hold the total
  var value = 0;      //  this holds the value of the vote cast

  for (var i=0; i < rankarray.length; i++){
    rank = rankarray[i];  
    if (rank = 1) {
    value = 3;
    }
    if (rank = 2) {
    value = 2;
    }
    if (rank = 3) {
    value = 1;
    }
    result += value;
  }

  return result;
}

编辑:

我想我已经解决了。 有人看到以下任何问题吗?

function tally(rankarray) {
  var rank = 0;
  var result = 0;     // this will hold the total
  var value = 0;      //  this holds the value of the vote cast

  for (var i=0; i < rankarray.length; i++){
    rank = rankarray[i];  
    if (rank == 1) {
      value = 3;
    }
    else if (rank == 2) {
      value = 2;
     }
    else if (rank == 3) {
      value = 1;
    }
    result += value;
    value = 0;
  }
  return result;
}

尝试以下脚本:

function tally(rankarray) 
{
  var rank = 0;       // this is the vote cast
  var result = 0;     // this will hold the total
  var value = 0;      //  this holds the value of the vote cast

  for (var i=0; i < rankarray.length; i++)
  {
    rank = rankarray[i];
    switch(rank)
    {
        case 1:
            value = 3;
            break;
        case 2:
            value = 2;
            break;
        case 3:
            value = 1;
            break;
    }
    result += value;
  }
  return result;
}

的jsfiddle。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM