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 = ["♣", "♥", "♦", "♠"];
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: "♠", Value: "2", Weight: undefined}
1: {Suit: "♥", Value: "10", Weight: undefined}
2: {Suit: "♣", Value: "5", Weight: undefined}
And I can't seem to understand why the Weight is undefined. Would appreciate any help Thanks!
intnot an array. changecardWeightas an integer:let cardWeight = parseInt(cardValues[j]);and later trying to index into it:Weight: cardWeight[j]