最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript if condition with return - Stack Overflow

programmeradmin1浏览0评论

Folks, I am modifying a current function slightly to make use of two variables. I have shown the past and present versions in the code snippet.
Basically what I want is if either one of the two if conditions namely First Condition and Second Condition are are true, do not execute the remaining logic of the function. If both are false, continue with the remaining code of the function.
I think I am making a silly mistake somewhere and if the first condition is true, execution is stopping right there. (I Know that is because of the return statement at the end.) How do I make sure the second if condition as well even if first one was true and returned a 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

}

Ealier this code used to be:

function myAlgorithm() {

   if((First Condition) && (Second Condition)){
    alert("You cant continue");

    return;
   }

  //Rest of the function logic goes here
}

Folks, I am modifying a current function slightly to make use of two variables. I have shown the past and present versions in the code snippet.
Basically what I want is if either one of the two if conditions namely First Condition and Second Condition are are true, do not execute the remaining logic of the function. If both are false, continue with the remaining code of the function.
I think I am making a silly mistake somewhere and if the first condition is true, execution is stopping right there. (I Know that is because of the return statement at the end.) How do I make sure the second if condition as well even if first one was true and returned a 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

}

Ealier this code used to be:

function myAlgorithm() {

   if((First Condition) && (Second Condition)){
    alert("You cant continue");

    return;
   }

  //Rest of the function logic goes here
}
Share asked Dec 2, 2011 at 18:45 user1052591user1052591 1852 gold badges2 silver badges8 bronze badges 5
  • 1 Do you want to eval both code1 and code2, no matter which conditions are true? And did you notice that the return in the second if is placed after the closing brace? – Yogu Commented Dec 2, 2011 at 18:48
  • Have you tried by doing return True; or return 1; in place of those empty return; – Zain Khan Commented Dec 2, 2011 at 18:48
  • @SLaks, because code1 and code2 are strings. – user1052591 Commented Dec 2, 2011 at 18:50
  • @Yogu, Fixed the return. Thanks for pointing out. – user1052591 Commented Dec 2, 2011 at 18:50
  • @Mangobug, I didnt try. Should I return 1 from both and later on check for 1? – user1052591 Commented Dec 2, 2011 at 18:51
Add a ment  | 

2 Answers 2

Reset to default 1

Use a variable and increment it once the condition is met. Then check to see if the variable was incremented.

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
}

Could use flag variables to keep track of your conditions and then check both of them like before

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

}
发布评论

评论列表(0)

  1. 暂无评论