He "always" draws 2 elements due to i+=text . Your array is small, so the loop requires 2 iterations (rolling the lines to get the number i ) << 22>.
First iteration: i = 0 => 0 < myArray.length => true prints number Second iteration: (say '4' get choosen) i = i + text and text = '4' => i = "04" => "04" < myArray.length => true prints number Third iteration: (say '3' get choosen) i = i + text and text = '43' => i = "0443" => "0443" < myArray.length => false loop breaks
Thus, it is possible to print two elements. Depending on the length of the array, it may be longer.
You do not need a loop, just select a number and type it:
function RandomDraw() { if(myArray.length > 0) { // if there still elements in the array var ri = Math.floor(Math.random() * myArray.length); // do your job ... var rs = myArray.splice(ri, 1); document.getElementById("showSplice").textContent = rs; // .textContent is better } else { // print a message indicating that the array is now empty } }
source share