簡體   English   中英

如何將數組的隨機元素添加到有序列表中?

[英]How to add random elements of an array to an ordered list?

我有一個由十個元素組成的數組,其中五個是隨機顯示的。 我想在有序列表中顯示這五個隨機元素。 目標是在更改隨機元素時,列表編號保持不變,不變。

我怎樣才能實現它?

我已經嘗試了一些方法,但我無法弄清楚隨機元素的存儲位置。

     <body>

     <button id="btn" onclick="randomValue()"> Change </button>

     <div id="container">

        <ol id="para"> </ol>

     </div>

     </body>

    var numbers = [

    "Why was the Happy prince weeping?<br>",
    "Why did the Happy Prince give the ruby?<br>",
    "What is the central idea of the poem 'Good Will'?<br>",
    "What was the ill child asking the mother for?<br>",
    "Why is hasty selling disadvantageous?<br>",
    "How should prudent spending of riches be done?<br>",
    "Who advised Wasserkof to get his fess refunded?<br>",
    "Why did the author refuse to go into Lucia's room?<br>",
    "Give a character sketch of Wasserkopf.<br>",
    "How did the war affect the family of the two boys?<br>"
  ];
       function getRandom(arr, count) {

        var shuffled = arr.slice(0),
            i = arr.length,
            min = i - count,
            temp, index;

        while (i-- > min) {

            index = Math.floor((i + 1) * Math.random());

            temp = shuffled[index];

            shuffled[index] = shuffled[i];

            shuffled[i] = temp;

        }

        return shuffled.slice(min);

       }

   function randomValue() {

    document.getElementById("para").innerHTML = getRandom(numbers, 5);

   }
  1. 給出Wasserkkopf的人物素描。

我希望在更改問題時數字“1”保持不變。

您可以1)生成隨機索引,2)對它們進行排序,3)然后將它們映射到數組值:

 function getRandom(arr, count) { let randomIndices = []; while (randomIndices.length < count) { let randomIndex = Math.floor(Math.random() * arr.length); if (!randomIndices.includes(randomIndex)) randomIndices.push(randomIndex); } randomIndices.sort(); return randomIndices.map(i => arr[i]); } let random = getRandom(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'], 3); console.log(random); 

這是一種方法。 您不需要元素末尾的<br>

 var numbers = [ "Why was the Happy prince weeping?", "Why did the Happy Prince give the ruby?", "What is the central idea of the poem 'Good Will'?", "What was the ill child asking the mother for?", "Why is hasty selling disadvantageous?", "How should prudent spending of riches be done?", "Who advised Wasserkof to get his fess refunded?", "Why did the author refuse to go into Lucia's room?", "Give a character sketch of Wasserkopf.", "How did the war affect the family of the two boys?" ]; let ul = document.getElementById("para"); function randomValue() { let copy = [...numbers]; ul.innerHTML = ''; for(var i=1; i<=5; i++) { let ixElement = Math.floor(Math.random() * copy.length); let li = document.createElement("li"); li.innerHTML = copy[ixElement]; ul.appendChild(li); copy.splice(ixElement, 1); } } randomValue(); 
 <button id="btn" onclick="randomValue()"> Change </button> <div id="container"> <ol id="para"> </ol> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM