Home > other >  set back value after for loop is completed
set back value after for loop is completed

Time:10-15

I've have following code with a problem.

After clicking my button in my template my counter will be incremented. Now I want to check if my counter is higher, lower or equal to a length of an array.

After doing my for-loops I want to set my counter back to zero.

I've tried with return this.counter = 0 - but this didn't work.

Thanks in advance for helping me out!

methods: {
  if(this.counter > this.lengthArray) { 
    for(let i = this.lengthArray; i <= this.counter; i  ) {
      //Do some code
    }
  } else if (this.lengthArray > this.counter) {
    for(let i = 1; i <= this.counter; i  ) {
      //Do some code as well
    }
  } else if (counter == 0) {
    //Process another code
  }
},

data() {
  return {
    counter: 0
  }
}

CodePudding user response:

You don't want the method to return the value this.counter = 0.

Setting this.counter = 0 after your last condition should work. Nothing should return or break the loops.

  • Related