0

I have a function that gets values using for loops and puts them into 1 variable. This variable is them stuck in a form where its is then submitted. But the problem is that the for loop would finish looping by the time the value is entered and submitted. So everytime I test it, the variable "value" = 9999. Can anyone please tell me how to add 1 to variable every time the function loops? Thanks in advance.

function myFunction() {

 var value1;
 var value2;
 var value3;
 var value4;
 var value;

 for (value1 = 0; value1 < 10; value1++) {
  for (value2 = 0; value2 < 10; value2++) {
   for (value3 = 0; value3 < 10; value3++) {
    for (value4 = 0; value4 < 10; value4++) {
     value = value1.toString() + value2.toString() + value3.toString() + value4.toString() + '@2018';
     document.getElementById('pswd').value = value;
     document.getElementById('uid').value = 'test';
     document.getElementById('commentForm').submit();
    }
   }
  }
 }
}

var loop = setInterval(function() {
 myFunction(); 
}, 2000);
Rob
  • 14,746
  • 28
  • 47
  • 65
Blackmagyk
  • 80
  • 5
  • *"Can anyone please tell me how to add 1 to variable every time the function loops?"* Which variable? – P.S. Jan 26 '18 at 01:40
  • 2
    Well it will submit on the first iteration of the inner loop.... not sure what you actually expect to happen... – epascarello Jan 26 '18 at 01:46
  • 2
    What is your real goal here? You just want a counter? – StackSlave Jan 26 '18 at 01:49
  • There is a general anti-pattern in programming that if you have loops nested more than 2 deep, you are probably not thinking about the problem correctly. – Scott Marcus Jan 26 '18 at 01:59
  • Your loops are simply incrementing a number from `0` to `9999` and converting it to a string with leading zeroes. But submitting a form reloads the page, which should stop your script. – Barmar Jan 26 '18 at 02:20
  • What are you trying to do here? It looks like you're trying to login to a form, trying all different 4-digit PINs. – Barmar Jan 26 '18 at 02:21
  • I want to add to the "value" variable – Blackmagyk Jan 26 '18 at 13:07

1 Answers1

1

If I'm reading your question correctly, you would like the number to go up by one each time the interval callback is called? You can accomplish this by keeping track of the value outside the function, and do a single increment each time the callback is called. However, you are using 4 variables to basically count from 0 to 9999. You can simplify that a lot by using one variable to increment. Then you can left pad it with zeroes. That would look like this.

var value = 0;

function myFunction() {
    var pswd = value.toString().padStart(4, 0) + '@2018';
    document.getElementById('pswd').value = pswd;
    document.getElementById('uid').value = 'test';
    document.getElementById('commentForm').submit();

    value++;
    if(value > 9999) {
        value = 0;
    }
}

var loop = setInterval(myFunction, 2000);

If you can't use padStart, you can use slice instead. You can replace that line with the following.

var pswd = ('0000' + value).slice(-4) + '@2018';
kamoroso94
  • 1,713
  • 1
  • 16
  • 19