0

Simple code, working on a card game, however I am encountering this error right at the beginning. I am trying to generate 52 random numbers and print out the appropriate array element associated with it. Here is my code:

    <!DOCTYPE html>
    <html>
    <head>

    </head>
    <body>
        <button onclick="shuffleCards()">Shuffle Cards</button>
        <script>
            function shuffleCards() {
                var cardDeck = ["1S", "1C", "1H", "1D", "2S", "2C", "2H", "2D", "3S", "3C", "3H", "3D", "4S", "4C", "4H", "4D", "5S", "5C", "5H", "5D", "6S", "6C", "6H", "6D", "7S", "7C", "7H", "7D", "8S", "8C", "8H", "8D", "9S", "9C", "9H", "9D", "10S", "10C", "10H", "10D", "11S", "11C", "11H", "11D", "12S", "12C", "12H", "12D", "13S", "13C", "13H", "13D", ];
                var handUser = [];
                var handCPU = [];
                var shuffledDeck = [];
                for (var i = 51; i < 1; i--) {
                    var randomNumber = Math.floor(Math.random()*(i + 1));
                    console.log(cardDeck[randomNumber]);
                }
            }
        </script>
    </body>
</html>
JSON_Derulo
  • 892
  • 2
  • 14
  • 39
  • 2
    In your for loop, `for (var i = 51; i < 1; i--)` i is never less than 1. Maybe it should be >1 – jgabb Mar 29 '16 at 21:16
  • Also, if you want each card appear only once, take a look at http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript – Aleph Aleph Mar 29 '16 at 21:18

5 Answers5

0

The for loop iterator, i, starts out greater than 1, and so it isn't less than 1. Change your operator to > instead of <

0

It's quite simple really, your for loop's condition is never true, it should be i >== 0. If i = 51 then i < 1 will never be true.

AGE
  • 3,752
  • 3
  • 38
  • 60
0

A problem with your solution is that it doesn't guarantee that a unique card will be returned each time, which I assume you want to guarantee.

If this is true, then this solution will iterate 52 times, each time taking and removing a card from the deck using the splice method:

var cardDeck = ["1S", "1C", "1H", "1D", "2S", "2C", "2H", "2D", "3S", "3C", "3H", "3D", "4S", "4C", "4H", "4D", "5S", "5C", "5H", "5D", "6S", "6C", "6H", "6D", "7S", "7C", "7H", "7D", "8S", "8C", "8H", "8D", "9S", "9C", "9H", "9D", "10S", "10C", "10H", "10D", "11S", "11C", "11H", "11D", "12S", "12C", "12H", "12D", "13S", "13C", "13H", "13D", ];
var total = cardDeck.length;
for (var i = 0; i < total; i++) {
  var randomPos = Math.floor((Math.random() * (cardDeck.length-1)));
  var randomCard = cardDeck.splice(randomPos, 1).pop();
  console.log(randomCard);
}
David Zorychta
  • 13,039
  • 6
  • 45
  • 81
  • Thanks for the quick response, I know it will not yet return a unique card. I am just trying to get this loop to function first before I splice out elements from the array with each iteration. Thanks for your help. – JSON_Derulo Mar 29 '16 at 21:35
0
<!DOCTYPE html>
<html>
<head>
<script>
function shuffleCards() {
  console.log('test')
  var cardDeck = ["1S", "1C", "1H", "1D", "2S", "2C", "2H", "2D", "3S",
  "3C", "3H", "3D", "4S", "4C", "4H", "4D", "5S", "5C", "5H", "5D", "6S", "6C",
  "6H", "6D", "7S", "7C", "7H", "7D", "8S", "8C", "8H", "8D", "9S", "9C", "9H", "9D",
  "10S", "10C", "10H", "10D", "11S", "11C", "11H", "11D", "12S", "12C", "12H", "12D",
  "13S", "13C", "13H", "13D" ];
  var handUser = [];
  var handCPU = [];
  var shuffledDeck = [];
  for (var i = 51; i > 0; i--) {
    var randomNumber = Math.floor(Math.random()*(i + 1));
    console.log(cardDeck[randomNumber]);
  }
}
</script>
</head>
<body>
  <button onclick="shuffleCards()">Shuffle Cards</button>
</body>
</html>

This works...

Arash Saidi
  • 2,228
  • 20
  • 36
0

Here's one more method for shuffling the cards. This method shows an approach that does not splice the original array. For an small array, it's quick and easy to clone the array and remove items from it as necessary, but if we were dealing with a really big array, when using an object to track used items is a good approach.

function shuffleCards() {
    var cardDeck = ["1S", "1C", "1H", "1D", "2S", "2C", "2H", "2D", "3S", "3C", "3H", "3D",
            "4S", "4C", "4H", "4D", "5S", "5C", "5H", "5D", "6S", "6C", "6H", "6D",
            "7S", "7C", "7H", "7D", "8S", "8C", "8H", "8D", "9S", "9C", "9H", "9D",
            "10S", "10C", "10H", "10D", "11S", "11C", "11H", "11D",
            "12S", "12C", "12H", "12D", "13S", "13C", "13H", "13D"];
    var used = {};
    var shuffledDeck = [];
    while (shuffledDeck.length < 52) {
        var randomNumber = Math.floor(Math.random() * (51 + 1));
        if (!used[randomNumber]) {
            shuffledDeck.push(cardDeck[randomNumber]);
            used[randomNumber] = true;
        }
    }
    console.log(shuffledDeck.length, shuffledDeck);
}
shuffleCards();
Will
  • 1,718
  • 3
  • 15
  • 23