[英]Return a function for IF ELSE IF statements
I am trying to run a block of code that is series of IF ELSE IF statements, that checks to see if certain conditions are true.我试图运行一个代码块,它是一系列 IF ELSE IF 语句,它检查某些条件是否为真。
if (2 > 1) {
return true;
}
else if (3 > 1) {
return true;
}
else if (4 > 1) {
return true;
}
else {
alert('Not all conditions are true')
}
It is working fine, but I want to add a function at the end, if all the conditions are true, In the code below I added the function, but now it seems to run the function without checking all the conditions first它工作正常,但我想在最后添加一个函数,如果所有条件都为真,在下面的代码中我添加了该函数,但现在它似乎没有先检查所有条件就运行该函数
if (2 > 1) {
return true;
}
else if (3 > 1) {
return true;
}
else if (4 > 1) {
return true;
}
else {
alert('Not all conditions are true')
}
// Run this function after checking all the conditions are true
allConditionsTrue();
I think I am getting confused with where the function should be placed.我想我对应该放置函数的位置感到困惑。
Any help is greatly appreciated :)任何帮助是极大的赞赏 :)
For an arbitrary count of condition and with an immediate exit if one previous condition is false
and the next is true
, you could take a variable for the state if someFalse
value occurs.对于条件和立即退出,如果一个以前的状态是一个任意的数量false
,下一个是true
,你可以,如果采用可变的状态someFalse
发生值。
Afetr checkin all conditions check someFalse
and return with 'Not all conditions are true'
. someFalse
checkin all conditions 检查someFalse
并返回'Not all conditions are true'
。
Finally it is clear, that all conditions are true
and you can call a function for this case.最后很明显,所有条件都为true
,您可以为这种情况调用一个函数。
function check(value) { function allConditionsTrue() { console.log('All conditions are true'); } let someFalse = false, conditions = [value < 2, value < 3, value < 4], fn = c => c ? someFalse : !(someFalse = true); if (conditions.some(fn)) return true; if (someFalse) { console.log('Not all conditions are true'); return false; } allConditionsTrue(); return true; } console.log(1, check(1)); console.log(2, check(2)); console.log(3, check(3)); console.log(4, check(4));
Take a look at && (AND) and ||看看 && (AND) 和 || (OR) operators: (OR) 运算符:
if (2>1 && 3>1 && 4>1) {
allConditionsTrue();
return true;
} else {
alert('not all conditions true');
}
You placed the function call outside of all the checks so it is independent of the conditions.您将函数调用置于所有检查之外,因此它与条件无关。 You can give it its own if block like:你可以给它自己的 if 块,如:
if (2 > 1 && 3 > 1 && 4 > 1) {
allConditionsTrue()
}
Or if you don't want to rewrite all the checks then you can create an array to hold their respective values like或者,如果您不想重写所有检查,则可以创建一个数组来保存它们各自的值,例如
let conds = [false, false, false]
if (2 > 1) {
conds[0] = true
}
if (3 > 1) {
conds[1] = true
}
if (4 > 1) {
conds[2] = true
}
if (!conds.some(v => !v)) { // all are true
allConditionsTrue()
}
if (conds.some(v => v)) {
return true
} else {
alert('Not all conditions are true')
}
Edit: corrected with suggestion from comment编辑:根据评论的建议进行更正
Edit 2: an improvement编辑2:改进
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.