Blackjack game: Count Hand Value method I am almost completing my BlackJack game in Javascript. But I realize that there must be a way to improve the performance for these two methods. Any pointers?

const hasAceInHand = (cardsOnHand) =&gt; {
    for (let key in cardsOnHand) {
        let arr = cardsOnHand[key];
        for (let i = 0; i &lt; arr.length; i++) {
            let obj = arr[i];
            for (let prop in obj) {
                if (prop === "face") {
                    if (obj[prop] === "A") {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

const countHandValue = (cardsOnHand) =&gt; {
    //console.log(hasAceInHand(cardsOnHand));
    let sum = 0;
    for (let key in cardsOnHand) {
        let arr = cardsOnHand[key];
        for (let i = 0; i &lt; arr.length; i++) {
            let obj = arr[i];
            for (let prop in obj) {
                if (prop === "faceValue") {
                    //console.log(prop + " = " + obj[prop]);
                    sum = sum + obj[prop];
                    debugger;
                    if (sum &gt; 21 &amp;&amp; hasAceInHand(cardsOnHand)) {
                        // Transfer Ace's face value from 11 to 1
                        sum = sum - 11;
                        sum = sum + 1;
                    }
                }
            }
        }
    }

    return sum;
}


This countHandValue method is called at below:

const cardSuit = ["hearts", "diams", "clubs", "spades"];
const cardFace = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];

// In JavaScript, functions are objects.
// You can work with functions as if they were objects.
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 deck = {
    cards: []
}

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

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

const dealOneCardToPlayer = () =&gt; {
    // Take a card from the top deck to be assigned to tempcard.
    tempCard = deck.cards.splice(0, 1);
    player.cards.push(tempCard);
    player.handValue = countHandValue(player.cards);
    document.getElementById("handValuePlayer").innerHTML = player.handValue;
    makeCardPlayer(tempCard[0]);
}


I have uploaded the whole program.
Review

Function hasAceInHand could be boiled down to a one-liner using array.find. It returns the first element of the array that passes the condition x =&gt; x.face === "A" provided.

const hasAceInHand = (cardsOnHand) =&gt; {
  return cardsOnHand.find(x =&gt; x.face === "A") != null;
}


Function countHandValue iterates the items, and calls the other function which iterates the items nested in the outer iteration if (sum &gt; 21 &amp;&amp; hasAceInHand(cardsOnHand)) { ... You could just keep track of aces to avoid iterating the items more than once.

Alternative for countHandValue

In black-jack, at most 1 ace could have value 11, all other aces in the hand must have value 1, else 21 would get exceeded (11 + 11 &gt; 21). I would change the default value of an ace to 1.

switch (face) {
    case "A":
        this.faceValue = 1;
        break;


As a first step, Array.reduce could be used to calculate the sum of card values.

const countHandValue = (cardsOnHand) =&gt; {
  return cardsOnHand.reduce((sum, current) =&gt; {
    sum += current.faceValue;
    return sum;
  }, 0);
}


Then we can implement a relaxation allowing at most 1 ace to get value 11.

const countHandValue = (cardsOnHand) =&gt; {
  let ace;
  let value = cardsOnHand.reduce((sum, current) =&gt; {
    ace |= current.faceValue === 1;
    sum += current.faceValue;
    return sum;
  }, 0);
  if (ace &amp;&amp; value + 10 &lt;= 21) {
    value += 10;
  }
  return value;
}


And we also no longer need to call hasAceInHand as we're tracking the ace inline.