Steve
Steve

Reputation: 4453

Javascript - Loop through object with array property

I am trying out a simple BlackJack game with JS. Here are some of my codes:

 function card(suit, face) {
    this.suit = suit;
    this.face = face;

    switch (face) {
        case "A":
            this.faceValue = 11;
            break;
        case "J":
        case "Q":
        case "K":
            this.faceValue = 10;
            break;
        default:
            this.faceValue = parseInt(face);
            break;
    }
};

const player = {
    cards: [],
    handValue: 0
}

const dealOneCardToPlayer = () => {     
    tempCard = deck.cards.splice(0, 1);
    player.cards.push(tempCard);
    player.handValue = countHandValue(player.cards);
}

I am stuck with countHandValue method which I couldn't get the faceValue of Cards for player object. I try with several types of for loop but with no success still. Once I can get the faceValue, then I can sum it up and assign to handValue property.

const countHandValue = (cardsOnHand) => {    
    for (const key of cardsOnHand) {
        console.log(key.faceValue);
    }

    for (const key in cardsOnHand) {
        console.log(key.faceValue);
    }
}

[Code Edited]I searched and found this code which i can retrieve my faceValue property but I believe there are much extra code:

    const countHandValue = (cardsOnHand) => {
    let sum = 0;
    for (var key in cardsOnHand) {
        var arr = cardsOnHand[key];
        for (var i = 0; i < arr.length; i++) {
            var obj = arr[i];
            for (var prop in obj) {
                if (prop === "faceValue") {
                    console.log(prop + " = " + obj[prop]);
                    sum = sum + obj[prop];                        
                }                    
            }
        }
    }        
    return sum;
}

Upvotes: 1

Views: 76

Answers (2)

Mulan
Mulan

Reputation: 135187

Do consider that A can score as 1 or 11. This program shows you how to handle that -

const add = (x = 0, y = 0) =>
  x + y

const sum = (a = []) =>
  a .reduce (add, 0)

const scoreCard = (face = "") =>
  face === "A"
    ? [ 11, 1 ]
: (face === "K") || (face === "Q") || (face === "J")
    ? [ 10 ]
: [ Number (face) ]

const allScores = ([ face = "", ...more ]) =>
  face === ""
    ? [ scoreCard (face) ]
    : allScores (more)
        .flatMap
          ( hand =>
              scoreCard (face) .map (v => [ v, ...hand ])
          )

const scoreHand = (...hand) =>
{ const [ best = "Bust" ] =
    allScores (hand)
      .map (sum)
      .filter (score => score <= 21)
  
  if (best === 21)
    return "Blackjack!"
  else
    return String (best)
}

console .log
  ( scoreHand ("A", "K")                     // Blackjack!
  , scoreHand ("A", "A", "K")                // 12
  , scoreHand ("A", "A", "K", "7")           // 19
  , scoreHand ("J", "4", "K")                // Bust
  , scoreHand ("A", "A", "A", "A", "K", "7") // Blackjack!
  )

Upvotes: 1

Jack Bashford
Jack Bashford

Reputation: 44087

You can just use reduce:

const coundHandValue = cards => cards.reduce((acc, { faceValue }) => acc + faceValue, 0);

Upvotes: 1

Related Questions