0

I am trying to make a blackjack game with html/css/javascript, but when I trigger my shuffleDeck function, 1 card in my deck becomes undefined.

Could anyone explain the reasoning for this?

enter image description here

let cardValues = ["A", 1, 2, 3, 4, 5, 6, 7, 8, 9, "J", "Q", "K"];
let cardSuits = ["hearts", "spades", "clubs", "diamonds"];
let deck = new Array();
let players = new Array();

window.onload = function() {
    makeDeck();
    shuffleDeck(deck);
    console.log(deck);    
}

function makeDeck() {
    deck = new Array();
    for (let i = 0; i < cardValues.length; i++)
    {
        for(let j = 0; j < cardSuits.length; j++)
        {
            let weight = parseInt(cardValues[i]);;
            if (cardValues[i] == "J" || cardValues[i] == "Q" || cardValues[i] == "K")
            {
                weight = 10;
            } else if (cardValues[i] == "A")
            {
                weight = 11;
            }
            let card = {cardValue: cardValues[i], cardSuit: cardSuits[j], cardWeight: weight}
            deck.push(card)
        }
    }
}

function shuffleDeck(deck) {
    for (let i = deck.length; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [deck[i], deck[j]] = [deck[j], deck[i]];
    }
}
  • Presumably shuffleDeck has out of bounds access. – jarmod Oct 04 '22 at 15:12
  • 2
    `for (let i = deck.length; i > 0; i--) {` --> `for (let i = deck.length - 1; i >= 0; i--) {` – 001 Oct 04 '22 at 15:12
  • With `for (let i = N; i > 0; i--) {` you are iterating from `N` (included) to `1`, while probably you mean to iterate from `N-1` to `0` – Christian Vincenzo Traina Oct 04 '22 at 15:16
  • Does this answer your question? [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – James Oct 04 '22 at 15:18

2 Answers2

0

The problem lies in your shuffle loop: You let i = deck.length; and use i to access a card position in the deck. But when length is 52, the indexes of the array go from 0 to 51, so you swap undefined into the cards.

This is a very frequent mistake, since the array indices start at 0 not 1.

So to correct this, simply substract one to your length, or start at 0, while i<length.

Salketer
  • 14,263
  • 2
  • 30
  • 58
0

Like multiple people have already mentioned, your shuffle loop needs to start from deck.length - 1:

function shuffleDeck(deck) {
  for (let i = deck.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [deck[i], deck[j]] = [deck[j], deck[i]];
  }
}

let cardValues = ["A", 1, 2, 3, 4, 5, 6, 7, 8, 9, "J", "Q", "K"];
let cardSuits = ["hearts", "spades", "clubs", "diamonds"];
let deck = new Array();
let players = new Array();

window.onload = function() {
    makeDeck();
    shuffleDeck(deck);
    console.log(deck);    
}

function makeDeck() {
    deck = new Array();
    for (let i = 0; i < cardValues.length; i++)
    {
        for(let j = 0; j < cardSuits.length; j++)
        {
            let weight = parseInt(cardValues[i]);;
            if (cardValues[i] == "J" || cardValues[i] == "Q" || cardValues[i] == "K")
            {
                weight = 10;
            } else if (cardValues[i] == "A")
            {
                weight = 11;
            }
            let card = {cardValue: cardValues[i], cardSuit: cardSuits[j], cardWeight: weight}
            deck.push(card)
        }
    }
}

function shuffleDeck(deck) {
    for (let i = deck.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [deck[i], deck[j]] = [deck[j], deck[i]];
    }
}
Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26