Repeat a step in a for loop in JS

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
Marius
Posts: 107

Repeat a step in a for loop in JS

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.

MarPlo Posts: 186
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
  }
}