我应该如何管理if else条件的代码?

时间:2014-04-04 03:23:38

标签: javascript loops

我有三种方法,例如:

function method1(){
   console.log('method1');
}
function method2(){
   console.log('method2');
}
function method3(){
   console.log('method3');
}

现在我将在if else条件中应用这些方法,如下所示:

for(var i=0;i<100;i++){
   //for first 4 iteration apply method1:
   if(i<4){
      method1();
   }
   //next fifth iteration apply method2:
   else if(i==4){
      method2();
   }
   //next 3 iteration apply method3: 
   else if(i>4 && i<8){
      method3();
   }
   //next ninth iteration apply method2:
   else if(i==8){
      method2();
   }
   //next 3 iteration apply method1:
   else if(i>8 && i<12){
      method1();
   }
   ...........
}

像这样我要进行100次迭代,或者说如果它更长,那么编码过程会更长,并且可能会令人恼火。

因此,我认为可能有一种更简单的方法来解决这个问题。

为了让这个概念更清楚我想要做的事情,我将向您展示一个图表,从中可以很容易地找到这个问题的解决方案:

enter image description here


您可以跳过阅读以下内容:

在准备这个问题的图表时,我想到了以下概念:

从上图中

  • 您可能知道前4次迭代都有方法1。
  • 接下来的第5(4),第9(8),第13(12),第17(16),第21(20),第25(24), 29th(28),等等迭代有方法2。
  • 从method2开始的下一次迭代,低于以下方法2 迭代有方法3。
  • 所有其他迭代都有method1。

for(var i=0; i<100; i++){
   //find the series 5th, 9th, 13th, and so on and apply method2:
   if( i == 5 + 4  * n){
      method2();
   }

   //find the series between 9th and 5th and so on and apply method3:
   else if( i > ( 5 + 4 * n ) && i < ( 5 + 4 * n - 1) ){
      method3();
   }

   //for other iterations apply method 1:
   else{
      method1();
   }
}//end for loop

所以,这几乎可能是在解决方案之后,但仍然混淆迭代到n。假设我将n置于for for(var i=0,n=0;i<100;i++)之类的循环中,那么它将迭代,就像我是5然后n是5然后5 + 4 * 5 = 25并且永远不会得到匹配。

for(var i=0,n=0; i<100; i++){
   if(i%4==0){
   n=-1;
}
//now i becomes equal when i is 5 then n is -1 at 4th i so 0 at 5th i and 5+4*0 = 5.... matched
//now check for next 9th i equals n: i is 9 then n is -1 at 8th i so 0 at 9th i and 5+4*0=5... not matched as this time it should be 1 for n
嗯,仍然很难解决这个问题,因为n的值应该是-1,在第4 i和0在第8 i和1在第12 i等等.....

仍然不确定我的其他条件是否有效。 所以,请帮我解决这个问题。

您可以尽可能轻松地走自己的路。

5 个答案:

答案 0 :(得分:2)

我认为你的程序遵循这种模式

for (var i = 0; i < 100; i += 1) {
    if (i % 4 === 0 && i) {
        method2();             // Current index is divisible by 4
    } else if (i % 8 < 4) {
        method1();             // Current index is within 0-3, 8-11, ...
    } else {
        method3();             // Current index is within 5-7, 13-15, ...
    }
}

答案 1 :(得分:1)

在这些情况下,我会这样做:

我会选择好的excel,在1列中是迭代器i,在另一列是我想要运行的方法。

然后我会编写一个宏来自动生成复制/粘贴它的代码。这需要时间,但如果这是可重复使用的,那么excel就成了你的设计和文档。

答案 2 :(得分:1)

你可以用下面的模式编写代码,然后解决你的数学问题。

var foo = {
  method1: function(){
    console.log('method1');
  },
  method2: function(){
    console.log('method2');
  },
  method3: function(){
    console.log('method3');
  }
}

var bar = function (num) {
  // put your formular here
  return num;
}

for(var i=0; i<100; i++){
  foo['method'+bar(i)]();
}

答案 3 :(得分:1)

for (var i = 0; i < 100; i++) {
    if (i > 0 && i % 4 == 0) {
        method2();
    }
    else if (parseInt(i / 4) % 2 == 0) {
        method1();
    }
    else {
        method3();
    }
}

看一下 fiddle example I've created

答案 4 :(得分:1)

您的图表显示最初只有method1执行4次(0到3之间)。之后有一个方法3和方法1的模式,每个模式执行3次,但是由方法2分开(每4次)。如果在系列结束之前就是这种情况,那么以下代码将起作用 -

if (nCtr < 4) {
   method1();
} else if ((nCtr % 4) == 0) {
    method2();
} else if (((nCtr / 4) % 2) == 0) {   // nCtr divided by 4 produces even number
    Method1();
} else {                              // nCtr divided by 4 produces odd number
    Method3();
}
相关问题