Home > Enterprise >  How to set for loop javascript custom order number
How to set for loop javascript custom order number

Time:03-17

How to create number like this in javascript loop

  • result '1' also result '1'
  • result '2' also result '2'
  • result '3' also result '3'
  • result '4' back to start result '1'
  • result '5' also result '2'
  • result '6' also result '3'
  • result '7' back to start result '1'
  • result '8' also result '2'
  • result '9' also result '3'
  • I'am sorry' i'am bad english

    CodePudding user response:

    let result = ""
    
    for(let i = 1 ; i <= 10 ; i  ){
    for(let j = 1 ; j <= 3 ; j  ){
    
       if(i === 10)
       {
         break;
       }
       
       
      if(i % 4 == 0)
      {
        result  = ' result '   i   ' back to start result '   j  '\n';
      }else{
        result  = ' result '   i   ' also result '   j  '\n';
      }
     
      if((i % 3) != 0)
      {
        i  ;
      }
      
    }
     if(i === 10)
      {
            break;
      }
    }
    
    console.log(result)

    CodePudding user response:

    I think you are looking for something like this

    const loop = (from, to, limit) => {
      for (let i = from; i <= to; i  ) {
        let number = i % limit === 0 ? limit : i % limit;
    
        //do something with the number
        console.log(number);
      }
    };
    

    Then if you call it like this:

    loop(1, 10, 3);
    

    The logged result is 1, 2, 3, 1, 2, 3, 1, 2, 3, 1

    • Related