Page 1 of 1

Repeat a step in a for loop in JS

Posted: 28 Nov 2020, 06:38
by Marius
I have a for() loop like this one which shows what i want to do.

Code: Select all

for(var i=0; i<15; i++) {
  // Add data in html
  // When data has a specified value, sets new data and repeat the loop
}
How can I repeat the for() loop when the parsed data has a a certain specified value?

Thanks in advance.

Repeat a step in a for loop in JS

Posted: 28 Nov 2020, 07:50
by MarPlo
To achieve your goal you can add the if() condition within the loop, and check the values of your data.
If it has the specified value, sets the new data you want, and decrement 1 from i to force the loop to repeat again.
Something like this:

Code: Select all

let specf_val ='your-value';
for(var i=0; i<15; i++) {
  // When data has a specified value
  if($data == specf_val){
    // sets new data

    i--; // to repeat the action
  } else {
    // Add data in html
  }
}