我正在尝试创建一个简单的谷歌电子表格功能,以转换排名到一个积分系统,然后添加积分。可以访问电子表格的人(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;
}发布于 2014-07-01 04:29:33
尝试以下脚本:
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.
希望这能有所帮助。
https://stackoverflow.com/questions/24498201
复制相似问题