I am trying to empty an array with a series of button onclick events.
How do I access the newly spliced array to run the same function over it until there are no more letters??
This is the code I am using:
(PS, I appreciate the community, comments, instructions, and help, but if you are gonna tell me how improper the code is, please think to rewrite it so everyone can learn the right way)
<body>
<button onClick="myFunction()">CLick</button>
</br>
<div id="myDiv"></div>
<div id="myDiv1"></div>
<button onClick="myFunction()">CLick</button>
</br>
<div id="myDiv2"></div>
<div id="myDiv3"></div>
<script>
function myFunction() {
Array.prototype.randsplice = function () {
var ri = Math.floor(Math.random() * this.length);
var rs = this.splice(ri, 1);
return rs;
};
var my_array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
var result = my_array.randsplice();
document.getElementById('myDiv').innerHTML=result;
document.getElementById('myDiv1').innerHTML=my_array;
}
</script>
</body>
Ok, Updated code thanks to Travis J and some add ons to his work. So I suppose technically this thread is over.
But... is there a better way to do the following?
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!-- type button as button so that it does not implicitly recieve submit as its type -->
<button id="myButton1" type="button">Click</button>
<br>
<div id="myDiv1"></div>
<div id="myDiv2"></div>
<!-- type button as button so that it does not implicitly recieve submit as its type -->
<button id="myButton2" type="button">Click</button>
<br>
<div id="myDiv3"></div>
<div id="myDiv4"></div>
<!-- type button as button so that it does not implicitly recieve submit as its type -->
<button id="myButton3" type="button">Click</button>
<br>
<div id="myDiv5"></div>
<div id="myDiv6"></div>
<!-- type button as button so that it does not implicitly recieve submit as its type -->
<button id="myButton4" type="button">Click</button>
<br>
<div id="myDiv7"></div>
<div id="myDiv8"></div>
<!-- type button as button so that it does not implicitly recieve submit as its type -->
<button id="myButton5" type="button">Click</button>
<br>
<div id="myDiv9"></div>
<div id="myDiv10"></div>
<!-- type button as button so that it does not implicitly recieve submit as its type -->
<button id="myButton6" type="button">Click</button>
<br>
<div id="myDiv11"></div>
<div id="myDiv12"></div>
<!-- type button as button so that it does not implicitly recieve submit as its type -->
<button id="myButton7" type="button">Click</button>
<br>
<div id="myDiv13"></div>
<div id="myDiv14"></div>
<script>
//Nothing wrong here, so lets just do it first
Array.prototype.randsplice = function () {
var ri = Math.floor(Math.random() * this.length);
return this.splice(ri, 1);//removed extra variable
};
//Use an eventlistener to wait for the DOM to load
//so that we can depend on DOM elements being available
window.addEventListener("load",function(){
//locate the button element and save it in a variable
var button1 = document.getElementById("myButton1");
var button2 = document.getElementById("myButton2");
var button3 = document.getElementById("myButton3");
var button4 = document.getElementById("myButton4");
var button5 = document.getElementById("myButton5");
var button6 = document.getElementById("myButton6");
var button7 = document.getElementById("myButton7");
//do the same for the divs
var myDiv1 = document.getElementById("myDiv1");
var myDiv2 = document.getElementById("myDiv2");
//do the same for the divs
var myDiv3 = document.getElementById("myDiv3");
var myDiv4 = document.getElementById("myDiv4");
//do the same for the divs
var myDiv5 = document.getElementById("myDiv5");
var myDiv6 = document.getElementById("myDiv6");
//do the same for the divs
var myDiv7 = document.getElementById("myDiv7");
var myDiv8 = document.getElementById("myDiv8");
//do the same for the divs
var myDiv9 = document.getElementById("myDiv9");
var myDiv10 = document.getElementById("myDiv10");
//do the same for the divs
var myDiv11 = document.getElementById("myDiv11");
var myDiv12 = document.getElementById("myDiv12");
//do the same for the divs
var myDiv13 = document.getElementById("myDiv13");
var myDiv14 = document.getElementById("myDiv14");
//setup the array we will use for this example
var my_array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
//show the original array values
myDiv2.innerHTML = my_array;
//attach a click event handler to our button
button1.onclick = function() {
//modify my_array and display result
myDiv1.innerHTML = my_array.randsplice();
myDiv2.innerHTML = my_array;
//attach a click event handler to our button
button2.onclick = function () {
//modify my_array and display result
myDiv3.innerHTML = my_array.randsplice();
myDiv4.innerHTML = my_array;
//attach a click event handler to our button
button3.onclick = function () {
//modify my_array and display result
myDiv5.innerHTML = my_array.randsplice();
myDiv6.innerHTML = my_array;
//attach a click event handler to our button
button4.onclick = function () {
//modify my_array and display result
myDiv7.innerHTML = my_array.randsplice();
myDiv8.innerHTML = my_array;
//attach a click event handler to our button
button5.onclick = function () {
//modify my_array and display result
myDiv9.innerHTML = my_array.randsplice();
myDiv10.innerHTML = my_array;
//attach a click event handler to our button
button6.onclick = function () {
//modify my_array and display result
myDiv11.innerHTML = my_array.randsplice();
myDiv12.innerHTML = my_array;
//attach a click event handler to our button
button7.onclick = function () {
//modify my_array and display result
myDiv13.innerHTML = my_array.randsplice();
myDiv14.innerHTML = my_array;
//check if the array is empty and if so, perhaps display a message
// if (my_array.length == 0) alert("Empty!");
};
}}}}}}});
</script>
</body>
</html>
Array.empty()method, or you want to know how to repeatedly call therandsplice()until the array is empty?