How can I manage my if else code if something else?

I have three methods, for example:

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

Now I will apply these methods in an if else condition, as shown below:

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();
   }
   ...........
}

Like me, I have to iterate to 100 or say if it is even longer, then the encoding process will be so long and can be annoying.

So, I thought there might be an easier way to solve this problem.

To make this concept even clearer what I wanted to do, I present to you a graph with which you could easily solve the following solution:

enter image description here


you can skip the following:

In preparing the schedule for this issue, the following concept occurred to me:

From the above chart

  • You may know that the first 4 iterations have method1.
  • 5- (4), 9- (8), 13- (12), 17- (16), 21- (20), 25- (24) 29 (28) .. 2.
  • method2 2 3.
  • 1.

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(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

Ummm, , n -1 4- 0 8- 1 12- .......

, . , , .

.

+4
5

,

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, ...
    }
}
+2

:

excel, 1 i, - , .

. , - , .

+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)]();
}
+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();
    }
}

, !

+1

Your graph shows that only initially method1 is executed 4 times (from 0 to 3). After that, there are templates for method 3 and method1 that execute 3 times each, but separated by method2 (every 4 times). If so until the end of the series, then the following code will work -

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();
}
+1
source

Source: https://habr.com/ru/post/1534956/


All Articles