I'm trying to take two names from my array of names, print them out, then remove those elements from my array and continue iterating until the array is empty.
var i;
function runNames() {
var names = ["John", "Joe", "Bobby", "Sam", "Don", "Lemo"];
for (i = names.length - 1; i >= 0; i = i - 2) {
if (typeof names !== undefined && names.length > 0) {
var item1 = 1;
var item2 = 1;
while (item1 == item2) {
item1 = Math.random() * i;
item2 = Math.random() * i;
item1 = Math.floor(item1);
item2 = Math.floor(item2);
}
document.getElementById("names").innerHTML += "<li>" + names[item1] + " " + names[item2] + "</li>";
names.splice(item1, 1);
names.splice(item2, 1);
}
}
}
<ul id="names"></ul>
<button onclick="runNames()">GENERATE TEAMS</button>
Console logs show the function working until the array empties, I guess I'm not handling that condition right. Thanks for any help.