My shuffle function is stopping at 26 for unknown reasons. How can I get it to shuffle all the cards?

user3291465

Okay, so I am pulling my hair out with this one. I am trying to make a function that will shuffle a virtual deck of cards. I saw examples online, but they were written in some syntax I am unfamiliar with. I really couldn't understand what was going on so I tried to write my own. Anyway, the way I am going about it, I am making a duplicate array and then randomly picking cards out of the first array and putting them into the second array one by one, then deleting the randomly selected card. Here's my code. The function is stopping once the length of the original array reaches 26.

shuffleDeck: function (deck) {
        var newDeck = deck;
        for (i = 0; i<newDeck.length;i++){
            randomIndex = Math.floor(Math.random() * deck.length);
            newDeck[i] = deck[randomIndex];
            deck.splice(randomIndex,1);  
            console.log(deck.length);
        }

        return newDeck;
    }
Chris Muench

Arrays are passed by reference in JavaScript so the splice is removing from the array which is why it stops

See http://orizens.com/wp/topics/javascript-arrays-passing-by-reference-or-by-value/

You can do

var newDeck = deck.slice(0);

For a copy

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

My shuffle function is stopping at 26 for unknown reasons. How can I get it to shuffle all the cards?

From Java

How can I shuffle an array?

From Dev

How can I shuffle a result I get from a Postgresql?

From Dev

How do I shuffle and deal cards one at a time to players?

From Dev

How can I shuffle bits efficiently?

From Dev

How can I shuffle the letters of a word?

From Dev

How can i shuffle RLMResults in swift?

From Dev

How can I shuffle the contents of a RealmResults object

From Dev

How can i shuffle RLMResults in swift?

From Dev

I want to shuffle 5 cards in an array

From Dev

How to shuffle a deck of cards memorised in an array?

From Dev

What are the disadvantages of my shuffle function?

From Dev

php shuffle a pack of cards

From Dev

Unable to shuffle cards randomly

From Dev

How can I get same result with same seed set at random.shuffle()

From Dev

How can I get same result with same seed set at random.shuffle()

From Dev

How to get all possible shuffle combinations using numpy

From Dev

How do I shuffle a deque?

From Dev

How can I shuffle the list with constraints(related to the index of each elements)

From Dev

How can I shuffle the list with constraints(related to the index of each elements)

From Dev

I've been trying to use the random.shuffle with tkinter. Can someone help me write a function to shuffle the colors on the buttons.

From Dev

Is this a sufficient way to shuffle a deck of cards?

From Dev

Is this a sufficient way to shuffle a deck of cards?

From Dev

How to shuffle cards within a Card object with Math.random()

From Dev

How to shuffle the answers in my array using a foreach?

From Dev

program that shuffles cards, when i hit the shuffle button the cards dissappear on the GUI windows

From Java

How do I shuffle an array in Swift?

From Dev

How do I shuffle the order of an array in Jekyll?

From Dev

How do I shuffle a multidimensional list in Python

Related Related

  1. 1

    My shuffle function is stopping at 26 for unknown reasons. How can I get it to shuffle all the cards?

  2. 2

    How can I shuffle an array?

  3. 3

    How can I shuffle a result I get from a Postgresql?

  4. 4

    How do I shuffle and deal cards one at a time to players?

  5. 5

    How can I shuffle bits efficiently?

  6. 6

    How can I shuffle the letters of a word?

  7. 7

    How can i shuffle RLMResults in swift?

  8. 8

    How can I shuffle the contents of a RealmResults object

  9. 9

    How can i shuffle RLMResults in swift?

  10. 10

    I want to shuffle 5 cards in an array

  11. 11

    How to shuffle a deck of cards memorised in an array?

  12. 12

    What are the disadvantages of my shuffle function?

  13. 13

    php shuffle a pack of cards

  14. 14

    Unable to shuffle cards randomly

  15. 15

    How can I get same result with same seed set at random.shuffle()

  16. 16

    How can I get same result with same seed set at random.shuffle()

  17. 17

    How to get all possible shuffle combinations using numpy

  18. 18

    How do I shuffle a deque?

  19. 19

    How can I shuffle the list with constraints(related to the index of each elements)

  20. 20

    How can I shuffle the list with constraints(related to the index of each elements)

  21. 21

    I've been trying to use the random.shuffle with tkinter. Can someone help me write a function to shuffle the colors on the buttons.

  22. 22

    Is this a sufficient way to shuffle a deck of cards?

  23. 23

    Is this a sufficient way to shuffle a deck of cards?

  24. 24

    How to shuffle cards within a Card object with Math.random()

  25. 25

    How to shuffle the answers in my array using a foreach?

  26. 26

    program that shuffles cards, when i hit the shuffle button the cards dissappear on the GUI windows

  27. 27

    How do I shuffle an array in Swift?

  28. 28

    How do I shuffle the order of an array in Jekyll?

  29. 29

    How do I shuffle a multidimensional list in Python

HotTag

Archive