I am trying to print out 20 sets of random 4-digit number, I used basic While loop for this but nothing is printed out. Can anyone catch an error on my code..?
// Function returns the four digit key codes between min-max
function generateKeyCode(min, max) {
// Randomize the key code with Math.random() function
var keyCode = Math.round((Math.random() * max) + min);
// Convert a number to a string with the toString() method.
// Use the length property of the text string.
var leadingZeroCount = 4 - keyCode.toString().length;
// Add leading zeros to keyCode, if needed (eg. 9 -> 0009)
// with loop
for (var j = 1; j <= leadingZeroCount; j++) {
keyCode = "0" + keyCode;
}
// Return four digit key code as a text
return keyCode;
}
// For hundred times
// Call generateKeyCode() function
// Print key code to the document
var output="";
var j=0;
var keyCodePrint = generateKeyCode(0,9999);
while(j<20) {
output += keyCodePrint;
j++;
}
document.getElementById("keycodes").innerHTML = output;
<div id="keycodes"/>
Aucun commentaire:
Enregistrer un commentaire