How do I get elements from an array without repetition?

Dave Birss

How do I get 5 elements from an array without repetition?

I'm trying to create an online version of story dice using Tumult Hype. All that I need to do is choose 5 image names from an array without repetition. But I just can't get it to work.

I've tried borrowing code from other stackoverflow answers and I can't get them working.

The code below is currently working but gives me repeats. How do I tinker with it to eliminate the repeats?

(You can see it in action here: https://davebirss.com/storydice/)

I know that it's probably verbose and inelegant. I only speak pidgin javascript, I'm afraid. And what I'm trying to do is currently beyond my ability.

Thank you so much in advance for your help.

var diceRoll = ['${resourcesFolderName}/story_dice1.png',
        '${resourcesFolderName}/story_dice2.png',
        '${resourcesFolderName}/story_dice3.png',
        ...,
        '${resourcesFolderName}/story_dice51.png']

function choose(n, arr) {
    while (arr.length > n) {
        var del = Math.floor(Math.random() * arr.length);
        arr = arr.filter(function(item, i) {
            return i !== del;
        });
    }
    return arr;}

var result1 = [choose(1, diceRoll)];
var result2 = [choose(1, diceRoll)];
var result3 = [choose(1, diceRoll)];
var result4 = [choose(1, diceRoll)];
var result5 = [choose(1, diceRoll)];

hypeDocument.getElementById("dice1").innerHTML = "<img src='"+result1+" 'height='125' width='125'>";
hypeDocument.getElementById("dice2").innerHTML = "<img src='"+result2+" 'height='125' width='125'>";
hypeDocument.getElementById("dice3").innerHTML = "<img src='"+result3+" 'height='125' width='125'>";
hypeDocument.getElementById("dice4").innerHTML = "<img src='"+result4+" 'height='125' width='125'>";
hypeDocument.getElementById("dice5").innerHTML = "<img src='"+result5+" 'height='125' width='125'>";

Update

Thank you all for your help. I'm sure all the answers were great but the snippet from U25lYWt5IEJhc3RhcmQg is the code that I managed to successfully incorporate. For the record, this is how I did it:

const rollTheDice = (arr, n) => {
  const randomN = [];
  while(randomN.length < n){
    const randomIndex = Math.floor(Math.random()*arr.length);
    randomN.push(arr[randomIndex]);
    arr.splice(randomIndex, 1);
  }
  return randomN;}

var result1 = (rollTheDice(images,1)); 
var result2 = (rollTheDice(images,1));
var result3 = (rollTheDice(images,1));
var result4 = (rollTheDice(images,1)); 
var result5 = (rollTheDice(images,1));

I've been repeatedly reloading the page and haven't seen any repeats yet. Perfect!

Yevgen Gorbunkov

I guess, the trickiest part here is not to waste the performance, limiting possible options to those, not previously chosen:

const images = ['a','b','c','d','e','f'];

const rollTheDice = (arr, n) => {
  const randomN = [];
  while(randomN.length < n){
    const randomIndex = Math.floor(Math.random()*arr.length);
    randomN.push(arr[randomIndex]);
    arr.splice(randomIndex, 1);
  }
  return randomN;
}

console.log(rollTheDice(images, 5));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can i get the number without repetition?

分類Dev

How do I get the type of an array's elements?

分類Dev

How do I get data from JSON Array MSSQL

分類Dev

MongoDB: how to get specific elements from array?

分類Dev

How do I get the attached element from the elements attached plugin data method?

分類Dev

How do I find elements in array with another indexes array?

分類Dev

How can I get a specific value from my JSON object without using Array.filter()?

分類Dev

How to get matching elements from array and change array object?

分類Dev

How do I unmarshal nested XML elements into an array?

分類Dev

How do I combine elements in an array matching a pattern?

分類Dev

How do I stop elements in an array being replaced every input?

分類Dev

How do I create an array from a forloop?

分類Dev

How do I sum up all remaing amount after get max value from array?

分類Dev

How do I get shuffle() from Collections to not modify every array in a list?

分類Dev

How do I pass array from Angular 6 to ASP.NET Core API for GET method?

分類Dev

How do I get the value from this API?

分類Dev

How do I get input from an alert?

分類Dev

How can I print multiple elements from an array inJavaSCript?

分類Dev

How can I remove all but the last N elements from an array?

分類Dev

How can I get the public elements from a Rust module?

分類Dev

How do I get nlargest rows without the sorting?

分類Dev

How do I get divs to spread out horizontally without wrapping?

分類Dev

how to get missing elements from array of Object? suppose my array of object is coming from server as Follow

分類Dev

What do I get if I declare an array without a size in global scope?

分類Dev

How can I create array elements dynamically and assign values (from variable with the same names) to the elements

分類Dev

How do I get the correct scale of a band array in GDAL Python?

分類Dev

How do I create dictionary from array of tuples?

分類Dev

How do I create a react list component from an array of objects?

分類Dev

How do I push data from a Subject observable to an array in the component?

Related 関連記事

  1. 1

    How can i get the number without repetition?

  2. 2

    How do I get the type of an array's elements?

  3. 3

    How do I get data from JSON Array MSSQL

  4. 4

    MongoDB: how to get specific elements from array?

  5. 5

    How do I get the attached element from the elements attached plugin data method?

  6. 6

    How do I find elements in array with another indexes array?

  7. 7

    How can I get a specific value from my JSON object without using Array.filter()?

  8. 8

    How to get matching elements from array and change array object?

  9. 9

    How do I unmarshal nested XML elements into an array?

  10. 10

    How do I combine elements in an array matching a pattern?

  11. 11

    How do I stop elements in an array being replaced every input?

  12. 12

    How do I create an array from a forloop?

  13. 13

    How do I sum up all remaing amount after get max value from array?

  14. 14

    How do I get shuffle() from Collections to not modify every array in a list?

  15. 15

    How do I pass array from Angular 6 to ASP.NET Core API for GET method?

  16. 16

    How do I get the value from this API?

  17. 17

    How do I get input from an alert?

  18. 18

    How can I print multiple elements from an array inJavaSCript?

  19. 19

    How can I remove all but the last N elements from an array?

  20. 20

    How can I get the public elements from a Rust module?

  21. 21

    How do I get nlargest rows without the sorting?

  22. 22

    How do I get divs to spread out horizontally without wrapping?

  23. 23

    how to get missing elements from array of Object? suppose my array of object is coming from server as Follow

  24. 24

    What do I get if I declare an array without a size in global scope?

  25. 25

    How can I create array elements dynamically and assign values (from variable with the same names) to the elements

  26. 26

    How do I get the correct scale of a band array in GDAL Python?

  27. 27

    How do I create dictionary from array of tuples?

  28. 28

    How do I create a react list component from an array of objects?

  29. 29

    How do I push data from a Subject observable to an array in the component?

ホットタグ

アーカイブ