javascript - 链接逻辑运算符在 javaScript 中给出错误结果

标签 javascript if-statement logical-operators

我正在尝试在网络应用程序中应用医疗评分。搜索功能没有给我满意的结果。
分数的一部分不是数学上的,而只是逻辑上确定的。 它需要三个变量,对这些变量的值进行检查和评分。

评分工作如下:

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 将这个分数实现到一个 Web 应用程序中。 我想出了一个计数函数 - 计算某个数字出现的次数。

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 编码还很陌生。因此,如果有人能向我解释我做错了什么,我将非常感激。

非常感谢您抽出时间。

最佳答案

我相信您的问题之一是您的函数 countThis(numberToCount) 没有 return 语句,因此返回 undefined

只需添加 return count; 就可以解决问题。

function countThis(numberToCount) {
    var count = 0;
    for (var i in arr) {
        if (arr[i] == numberToCount) {
            count++;
        }
    }
    return count;
}

如果您以相反的方式执行 if block ,从 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/

编辑:

再次查看您的代码后,我意识到您可能还需要考虑其他几个问题:

如果您希望函数正常工作,您应该声明函数内使用的所有变量,并仅返回您需要的内容。

而不是:

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

关于javascript - 链接逻辑运算符在 javaScript 中给出错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31472518/

相关文章:

javascript - Backbone.js 基于url片段的状态管理/ View 初始化

javascript - 对齐文章元素

csv - Powershell:如果 CSV 条目为空,如何引发错误

c - 在 if 指令中读取 c 中的字符串

binary - 三元计算机 : what would the third (unknown) part of a trit be used for?

c++ - 循环条件中逻辑运算符的使用

javascript - 让 Magnific Popup 与 RequireJS 一起使用

php - 在 php 中检索 Get Http 请求

mysql - SQL 存储过程 : If statement not behaving as expected

python - Python 中 AndAlso (&&) 和 OrElse (||) 逻辑运算符的等效项是什么?