Reputation: 17
good morning and thanks in advance to the whole community for this magnificent support. Who can help me to improveI a function to this code to return only 5 elements randomly from the entire list of 20 elements. I enter the code that now shows randomly the entire list. Thanks to the whole community...
var list = document.getElementById("items"),
newlist = document.getElementById("shuffle");
function shuffle(items)
{
var cached = items.slice(0), temp, i = cached.length, rand;
while(--i)
{
rand = Math.floor(i * Math.random());
temp = cached[rand];
cached[rand] = cached[i];
cached[i] = temp;
}
return cached;
}
function shuffleNodes()
{
var nodes = list.children, i = 0;
nodes = Array.prototype.slice.call(nodes);
nodes = shuffle(nodes);
while(i < nodes.length)
{
list.appendChild(nodes[i]);
++i;
}
}
window.onload = shuffleNodes;
<form id="something">
<div class="classname">
<dl id="items">
<dd>1. item.<br></dd>
<dd>2. item.<br></dd>
<dd>3. item.<br></dd>
<dd>4. item.<br></dd>
<dd>5. item.<br></dd>
<dd>6. item.<br></dd>
<dd>7. item.<br></dd>
<dd>8. item.<br></dd>
<dd>9. item.<br></dd>
<dd>10. item.<br></dd>
<dd>11. item.<br></dd>
<dd>12. item.<br></dd>
<dd>13. item.<br></dd>
<dd>14. item.<br></dd>
<dd>15. item.<br></dd>
<dd>16. item.<br></dd>
<dd>17. item.<br></dd>
<dd>18. item.<br></dd>
<dd>19. item.<br></dd>
<dd>20. item.<br></dd>
</dl>
</div>
</form>
Upvotes: 0
Views: 59
Reputation: 17
Good Morning, and thanks for the code! could you give me one last help? I'm trying but I can not divide this list (total 20 items); select random elements; Mix: 2 elements (between 0.10) and 2 elements (between 10.20) and show everything randomly when the page loads... in the snipped the list is divided (between 0 and 10) and the first 2 elements are selected....
var list = document.getElementById("items"),
newlist = document.getElementById("shuffle");
function shuffle(items)
{
var cached = items.slice(0,10), temp, i = cached.length, rand;
while(--i)
{
rand = Math.floor(i * Math.random());
temp = cached[rand];
cached[rand] = cached[i];
cached[i] = temp;
}
return cached;
}
function shuffleNodes()
{
var nodes = list.children, i = 0;
nodes = Array.prototype.slice.call(nodes);
nodes = shuffle(nodes);
list.innerHTML = "";
while(i < 2)
{
list.appendChild(nodes[i]);
++i;
}
}
window.onload = shuffleNodes;
<form id="something">
<div class="classname">
<dl id="items">
<dd>1. item.<br></dd>
<dd>2. item.<br></dd>
<dd>3. item.<br></dd>
<dd>4. item.<br></dd>
<dd>5. item.<br></dd>
<dd>6. item.<br></dd>
<dd>7. item.<br></dd>
<dd>8. item.<br></dd>
<dd>9. item.<br></dd>
<dd>10. item.<br></dd>
<dd>11. item.<br></dd>
<dd>12. item.<br></dd>
<dd>13. item.<br></dd>
<dd>14. item.<br></dd>
<dd>15. item.<br></dd>
<dd>16. item.<br></dd>
<dd>17. item.<br></dd>
<dd>18. item.<br></dd>
<dd>19. item.<br></dd>
<dd>20. item.<br></dd>
</dl>
</div>
</form>
Upvotes: 0
Reputation: 739
If you want to stay only 5 shuffled elements on the screen you should modify your shuffleNodes() by clearing the list and adding 5 elements in while loop as follows:
function shuffleNodes()
{
var nodes = list.children, i = 0;
nodes = Array.prototype.slice.call(nodes);
nodes = shuffle(nodes);
list.innerHTML = "";
while(i < 5)
{
list.appendChild(nodes[i]);
++i;
}
}
Upvotes: 1