Home > database >  TS: Boolean for for loop equals true on line before, still won't run
TS: Boolean for for loop equals true on line before, still won't run

Time:06-24

This function to label inside of a range using a for loop will not run the logic inside the for loop at all. Previously, I was defining the arguments for the for loop more dynamically, using indexing, and it was consuming all of my memory running a multimillion length loop. Completely unsure how this loop is not running.

      selectMonth(newMonth: number) {
        console.log(newMonth);
        const NMH: number[] = [ 744, 1416, 2160, 2800, 3624, 4344, 5088, 5832, 6552, 7296, 8016];
        const WH: number = 168;
        let labelValue: Label[] = ['Sunday'];
        // let i = NMH[newMonth];
        let WC = 0;
        let loopSize = NMH[newMonth   1] - NMH[newMonth];
        console.log(" loopSize" , loopSize);
        let i = 0;
        console.log("i right before" , i, i < loopSize)
        for(i; i  ; i < loopSize) {
          console.log("inside loop" , i)
        }
        console.log(labelValue);
  }

console print out of this function

CodePudding user response:

Just swap conditional statement i < loopSize and step i .

      selectMonth(newMonth: number) {
        console.log(newMonth);
        const NMH: number[] = [ 744, 1416, 2160, 2800, 3624, 4344, 5088, 5832, 6552, 7296, 8016];
        const WH: number = 168;
        let labelValue: Label[] = ['Sunday'];
        // let i = NMH[newMonth];
        let WC = 0;
        let loopSize = NMH[newMonth   1] - NMH[newMonth];
        console.log(" loopSize" , loopSize);
        let i = 0;
        console.log("i right before" , i, i < loopSize)
        for(i; i < loopSize;i  ) {
          console.log("inside loop" , i)
        }
        console.log(labelValue);
  }

CodePudding user response:

I just realized that the arguments are flipped around. For loop should be initiated with:

for(i; i  ; i < loopSize) {

There are some dumb questions haha

  • Related