链接逻辑运算符在 javaScript 中给出了错误的结果
Chaining logical operators gives wrong result in javaScript
我正在尝试在 Web 应用程序中应用医疗评分。搜索功能没有给我满意的结果。
分数的一部分不是数学上的,而只是 逻辑上 确定的。
它需要三个变量,对这些变量的值进行检查和评分。
评分是这样的:
Score Grades
0 <-- All 0
1 <-- At least one 1, but no >1
2 <-- 2 in only one region (respectively the variable)
3 <-- 2 in more than one region (respectively the variable)
4 <-- 3 in one or more region (respectively the variable)
5 <-- 4 in only one region (respectively the variable)
6 <-- 4 in more than one region (respectively the variable)
去年我开发了一个 Python 脚本,该脚本可以正常运行,但我想通过 javaScript 将这个分数实现到网络应用程序中。
我想到了一个计数函数——计算某个数字出现了多少次。
arr = [];
var a = 2;
var b = 4;
var c = 0;
arr.push(a, b, c);
function countThis(numberToCount) {
count = 0;
for (var i in arr) {
if (arr[i] == numberToCount) {
count++;
}
}
}
这些行似乎有效。但现在让我烦恼的部分来了。
我设置了一堆条件 - 所有条件都链接并与上面的函数结合 - 来计算分数。
function scoreArr() {
score = 0;
if (a === 0 && b === 0 && c === 0) {
score = 0;
} else if (a <= 1 && b <= 1 && c <= 1 && countThis(1) >= 1) {
score = 1;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) >= 2) {
score = 6;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) == 1) {
score = 5;
} else if (a <= 3 || b <= 3 || c <= 3 && countThis(3) >= 1) {
score = 4;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) > 1) {
score = 3;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) == 1) {
score = 2;
}
return score;
}
a = 0, b = 0, c = 0
给出了0
的正确分数。
但是对于所有其他可能的组合(数字 0 到 4),我得到完全相同的结果:6
。这显然是 错误的 。
我尝试更改运算符、添加括号……但没有任何效果。
我认为问题隐藏在我的条件中(?),但我不知道如何解决它。
这里是JSFIDDLE。
我必须承认我对编码还很陌生 javaScript。所以如果有人能向我解释我做错了什么,我将不胜感激。
非常感谢您的宝贵时间。
获得6分的可能原因是:
您已添加条件
说第 1 组
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) >= 2) {
score = 6;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) == 1) {
score = 5;
之前这个
说第 2 组
} else if (a <= 3 || b <= 3 || c <= 3 && countThis(3) >= 1) {
score = 4;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) > 1) {
score = 3;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) == 1) {
score = 2;
无论a
的值有多小,第1组的条件将首先执行,第2组[=39] =] 如果 a = 1 or 2 or 3
永远不会得到执行事件,因为这些所有值都满足条件 a <= 4
要修复它,您需要将条件移动为
function scoreArr() {
score = 0;
if (a === 0 && b === 0 && c === 0) {
score = 0;
} else if (a <= 1 && b <= 1 && c <= 1 && countThis(1) >= 1) {
score = 1;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) == 1) {
score = 2;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) > 1) {
score = 3;
} else if (a <= 3 || b <= 3 || c <= 3 && countThis(3) >= 1) {
score = 4;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) == 1) {
score = 5;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) >= 2) {
score = 6;
}
return score;
}
这是您更新后的 Fiddle
我认为您的问题之一是您的函数 countThis(numberToCount)
没有 return 语句,returning undefined
.
也是如此
只需添加 return count;
即可解决问题。
function countThis(numberToCount) {
var count = 0;
for (var i in arr) {
if (arr[i] == numberToCount) {
count++;
}
}
return count;
}
如果您以相反的方式执行 if 块,从 6-0 检查,每个 if 语句需要一个比较,这也会使您的代码更清晰,更不容易出错。这是您之前出错的地方,只需一点点重新安排和规划就能有所帮助:
function scoreArr() {
var score = 0;
if (countThis(4)>1) {
score = 6;
} else if (countThis(4)==1) {
score = 5;
} else if (countThis(3)>=1) {
score = 4;
} else if (countThis(2)>1) {
score = 3;
} else if (countThis(2)==1) {
score = 2;
} else if (countThis(1)>=1) {
score = 1;
} else {
score = 0;
}
return score;
}
我做了一个 jsFiddle,所以你可以看到结果:https://jsfiddle.net/Daniel300/evLqzuvs/5/
编辑:
再次查看您的代码后,我意识到您可能还想查看其他几个问题:
如果你想让你的函数正常工作,你应该声明你在函数内部使用的所有变量,并且只是return你需要的。
而不是:
addOne(1);
function addOne(num) {
result=num+1;
}
alert(result);
尝试:
function addOne(num) {
var result = num + 1; //Local variable, keeps it inside the function only.
return result; //Returns only what you need
}
alert(addOne(1)); //MUCH simpler :)
//alert(result); would give undefined
我正在尝试在 Web 应用程序中应用医疗评分。搜索功能没有给我满意的结果。
分数的一部分不是数学上的,而只是 逻辑上 确定的。
它需要三个变量,对这些变量的值进行检查和评分。
评分是这样的:
Score Grades
0 <-- All 0
1 <-- At least one 1, but no >1
2 <-- 2 in only one region (respectively the variable)
3 <-- 2 in more than one region (respectively the variable)
4 <-- 3 in one or more region (respectively the variable)
5 <-- 4 in only one region (respectively the variable)
6 <-- 4 in more than one region (respectively the variable)
去年我开发了一个 Python 脚本,该脚本可以正常运行,但我想通过 javaScript 将这个分数实现到网络应用程序中。 我想到了一个计数函数——计算某个数字出现了多少次。
arr = [];
var a = 2;
var b = 4;
var c = 0;
arr.push(a, b, c);
function countThis(numberToCount) {
count = 0;
for (var i in arr) {
if (arr[i] == numberToCount) {
count++;
}
}
}
这些行似乎有效。但现在让我烦恼的部分来了。
我设置了一堆条件 - 所有条件都链接并与上面的函数结合 - 来计算分数。
function scoreArr() {
score = 0;
if (a === 0 && b === 0 && c === 0) {
score = 0;
} else if (a <= 1 && b <= 1 && c <= 1 && countThis(1) >= 1) {
score = 1;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) >= 2) {
score = 6;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) == 1) {
score = 5;
} else if (a <= 3 || b <= 3 || c <= 3 && countThis(3) >= 1) {
score = 4;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) > 1) {
score = 3;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) == 1) {
score = 2;
}
return score;
}
a = 0, b = 0, c = 0
给出了0
的正确分数。
但是对于所有其他可能的组合(数字 0 到 4),我得到完全相同的结果:6
。这显然是 错误的 。
我尝试更改运算符、添加括号……但没有任何效果。 我认为问题隐藏在我的条件中(?),但我不知道如何解决它。
这里是JSFIDDLE。
我必须承认我对编码还很陌生 javaScript。所以如果有人能向我解释我做错了什么,我将不胜感激。
非常感谢您的宝贵时间。
获得6分的可能原因是:
您已添加条件
说第 1 组
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) >= 2) {
score = 6;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) == 1) {
score = 5;
之前这个
说第 2 组
} else if (a <= 3 || b <= 3 || c <= 3 && countThis(3) >= 1) {
score = 4;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) > 1) {
score = 3;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) == 1) {
score = 2;
无论a
的值有多小,第1组的条件将首先执行,第2组[=39] =] 如果 a = 1 or 2 or 3
永远不会得到执行事件,因为这些所有值都满足条件 a <= 4
要修复它,您需要将条件移动为
function scoreArr() {
score = 0;
if (a === 0 && b === 0 && c === 0) {
score = 0;
} else if (a <= 1 && b <= 1 && c <= 1 && countThis(1) >= 1) {
score = 1;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) == 1) {
score = 2;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) > 1) {
score = 3;
} else if (a <= 3 || b <= 3 || c <= 3 && countThis(3) >= 1) {
score = 4;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) == 1) {
score = 5;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) >= 2) {
score = 6;
}
return score;
}
这是您更新后的 Fiddle
我认为您的问题之一是您的函数 countThis(numberToCount)
没有 return 语句,returning undefined
.
只需添加 return count;
即可解决问题。
function countThis(numberToCount) {
var count = 0;
for (var i in arr) {
if (arr[i] == numberToCount) {
count++;
}
}
return count;
}
如果您以相反的方式执行 if 块,从 6-0 检查,每个 if 语句需要一个比较,这也会使您的代码更清晰,更不容易出错。这是您之前出错的地方,只需一点点重新安排和规划就能有所帮助:
function scoreArr() {
var score = 0;
if (countThis(4)>1) {
score = 6;
} else if (countThis(4)==1) {
score = 5;
} else if (countThis(3)>=1) {
score = 4;
} else if (countThis(2)>1) {
score = 3;
} else if (countThis(2)==1) {
score = 2;
} else if (countThis(1)>=1) {
score = 1;
} else {
score = 0;
}
return score;
}
我做了一个 jsFiddle,所以你可以看到结果:https://jsfiddle.net/Daniel300/evLqzuvs/5/
编辑:
再次查看您的代码后,我意识到您可能还想查看其他几个问题:
如果你想让你的函数正常工作,你应该声明你在函数内部使用的所有变量,并且只是return你需要的。
而不是:
addOne(1);
function addOne(num) {
result=num+1;
}
alert(result);
尝试:
function addOne(num) {
var result = num + 1; //Local variable, keeps it inside the function only.
return result; //Returns only what you need
}
alert(addOne(1)); //MUCH simpler :)
//alert(result); would give undefined