Forum Discussion
TomMusto
Qrew Cadet
If you can fix the function to always generate a random string that is 15 characters long, you won't need to refresh the field, right?
Here's how you can fix the function:
In your for loop, you need to have i<15 instead of i<16. This is because you start at i=0 and i is incremented only after the end of the looped code is reached. On the 15th iteration, i=14 so you want to stop the loop after that iteration.
The reason you're seeing variable string lengths is because of this formula:
By adding 1, you can occasionally generate a number that will cause you to look for a position in the string that is actually outside of the string (e.g. you're looking for the 37th character in a 36 character string). Math.random generates a random number between 0 and 1 that includes 0 but DOES NOT INCLUDE 1. Thus, the maximum number you can generate with the edited formula is 35 due to Math.Floor, but this is ok because charAt is 0-indexed! The last character in the string would be obtained by charAt(35) and so there is no reason to add 1 at the end.
If you make both of these changes, I believe you should be able to consistently generate 15 character random strings. If you want to get fancy, you can adjust the code to accept a length parameter and set up your function based on that so that you could later change to an 8 character string or a 37 character string if necessary. Disclaimer: I did not test this code with the changes I propose above, but if memory serves, this should get you the correct output.
------------------------------
-Tom
------------------------------
Here's how you can fix the function:
In your for loop, you need to have i<15 instead of i<16. This is because you start at i=0 and i is incremented only after the end of the looped code is reached. On the 15th iteration, i=14 so you want to stop the loop after that iteration.
The reason you're seeing variable string lengths is because of this formula:
Math.floor(Math.random() * chars.length + 1);
By adding 1, you can occasionally generate a number that will cause you to look for a position in the string that is actually outside of the string (e.g. you're looking for the 37th character in a 36 character string). Math.random generates a random number between 0 and 1 that includes 0 but DOES NOT INCLUDE 1. Thus, the maximum number you can generate with the edited formula is 35 due to Math.Floor, but this is ok because charAt is 0-indexed! The last character in the string would be obtained by charAt(35) and so there is no reason to add 1 at the end.
If you make both of these changes, I believe you should be able to consistently generate 15 character random strings. If you want to get fancy, you can adjust the code to accept a length parameter and set up your function based on that so that you could later change to an 8 character string or a 37 character string if necessary. Disclaimer: I did not test this code with the changes I propose above, but if memory serves, this should get you the correct output.
------------------------------
-Tom
------------------------------
MikailKote
5 years agoQrew Trainee
That worked perfectly Thank you :D
Changed it to:
------------------------------
Mikail Kote
------------------------------
Changed it to:
function makePasswd() { var passwd = ''; var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; for (i = 0; i<15; i++) { var c = Math.floor(Math.random() * chars.length + 0); passwd += chars.charAt(c) } return passwd; } $("#_fid_18").val(makePasswd());
------------------------------
Mikail Kote
------------------------------