1

I'm currently working on my first JS project, a BlackJack game. I've written a function to create a deck, deck is an array of objects containing card's Suit, Value and Weight. As for the Weight, after looping the function returns undefined, Suit and Value are returned fine. Here is my code:

// Creation of the deck
function deckCreate() {
  for (let i = 0; i < cardSuits.length; i++) {
    for (let j = 0; j < cardValues.length; j++) {
      let cardWeight = parseInt(cardValues[j]);
      if (cardValues[j] === "J" || cardValues[j] === "Q" || cardValues[j] === "K") {
        cardWeight = 10;
      } if (cardValues[j] === "A") {
        cardWeight = 11;
      }
      let card = {
        Suit: cardSuits[i],
        Value: cardValues[j],
        Weight: cardWeight[j]
      }; deck.push(card);
    }
  } return deck;
}

Beforehand I have the following variables created:

const cardSuits = ["&clubs;", "&hearts;", "&diams;", "&spades;"];
const cardValues = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
let deck = [];

The objects that are returned are: e.g.

0: {Suit: "&spades;", Value: "2", Weight: undefined}
1: {Suit: "&hearts;", Value: "10", Weight: undefined}
2: {Suit: "&clubs;", Value: "5", Weight: undefined}

And I can't seem to understand why the Weight is undefined. Would appreciate any help Thanks!

4
  • 1
    Please provide all the related code so that we can run your example and reproduce your issue. Commented Dec 20, 2018 at 22:23
  • 1
    cardWeight is an int not an array. change Commented Dec 20, 2018 at 22:24
  • 1
    You are defining cardWeight as an integer: let cardWeight = parseInt(cardValues[j]); and later trying to index into it: Weight: cardWeight[j] Commented Dec 20, 2018 at 22:24
  • 1
    @MarkMeyer have changed it to "Weight: cardWeight", works now, thanks! Commented Dec 20, 2018 at 22:30

3 Answers 3

2

The cardWeight is a number and not an array, which you get from this line:

// You are pulling a single value from an array here:
let cardWeight = parseInt(cardValues[j]);

so when you try to pass an index to it, you get undefined returned. Just access cardWeight as a single value:

// Creation of the deck
let cardSuits = ["&Clubs", "&Spades", "&Diamonds", "&Hearts"];
let cardValues = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"];
let deck = [];

function deckCreate() {
  for (let i = 0; i < cardSuits.length; i++) {
    for (let j = 0; j < cardValues.length; j++) {
      let cardWeight = parseInt(cardValues[j]);
      if (cardValues[j] === "J" || cardValues[j] === "Q" || cardValues[j] === "K") {
        cardWeight = 10;
      } if (cardValues[j] === "A") {
        cardWeight = 11;
      }
      let card = {
        Suit: cardSuits[i],
        Value: cardValues[j],
        Weight: cardWeight // <-- This is a single value, not an array
      }; deck.push(card);
    }
  } return deck;
}

console.log(deckCreate());

Sign up to request clarification or add additional context in comments.

Comments

2

Corrected, removing [j]

// Creation of the deck
function deckCreate() {
  for (let i = 0; i < cardSuits.length; i++) {
    for (let j = 0; j < cardValues.length; j++) {
      let cardWeight = parseInt(cardValues[j]);
      if (cardValues[j] === "J" || cardValues[j] === "Q" || cardValues[j] === "K") {
        cardWeight = 10;
      } if (cardValues[j] === "A") {
        cardWeight = 11;
      }
      let card = {
        Suit: cardSuits[i],
        Value: cardValues[j],
        Weight: cardWeight
      }; deck.push(card);
    }
  } return deck;
}

const cardSuits = ["&clubs;", "&hearts;", "&diams;", "&spades;"];
const cardValues = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
var deck = [];

console.log(deckCreate());

Comments

1

Change Weight: cardWeight[j] to Weight: cardWeight

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.