伙计们,我正在修改一个当前的功能,以便使用两个
变量。我在代码片段中显示了过去和现在的版本。
基本上我想要的是两个if conditions
中的任何一个,即 First Condition 和 Second Condition
是的,不要执行函数的剩余逻辑。如果两者都为假,则继续执行该函数的剩余代码。
我想我正在制作一个
在某处愚蠢的错误,如果第一个条件成立,执行就在那里停止。
(我知道这是因为最后的回复声明。)
即使第一个条件为真,我如何确保第二个if条件并返回return
function myAlgorithm(code1, code2) {
if(eval(code1)) {
if(First condition) {
alert("You cant continue");
return;
}
}
if(eval(code2)) {
if(Second condition){
alert("You cant continue");
return;
}
}
//If both of the above if conditions say "You cant continue", then only
//disrupt the function execution, other wise continue with the left
//logic
//Rest of the function logic goes here
}
以前的代码是:
function myAlgorithm() {
if((First Condition) && (Second Condition)){
alert("You cant continue");
return;
}
//Rest of the function logic goes here
}
答案 0 :(得分:1)
使用变量并在满足条件后递增。然后检查变量是否增加。
function myAlgorithm(code1, code2) {
var count = 0;
if (eval(code1)) {
if (First condition) {
alert("You cant continue");
count++;
}
}
if (eval(code2)) {
if (Second condition) {
alert("You cant continue");
count++;
}
}
if (count == 2) {
return "both conditions met";
}
//If both of the above if conditions say "You cant continue", then only
//disrupt the function execution, other wise continue with the left
//logic
//Rest of the function logic goes here
}
答案 1 :(得分:0)
可以使用标志变量来跟踪您的情况,然后像之前一样检查它们
function myAlgorithm(code1, code2) {
var flag1;
var flag2
if(eval(code1)) {
flag1 = First condition
}
if(eval(code2)) {
flag2 = second condition
}
if(flag1 && flag2){
return;
}
//If both of the above if conditions say "You cant continue", then only
//disrupt the function execution, other wise continue with the left
//logic
//Rest of the function logic goes here
}