cjl85
cjl85

Reputation: 165

Javascript - Reduce Method over array. Add point value of cards together to get a total

Currently, it is console logging:

14
16
[ { suit: '♣', value: 'J', points: 10 },
  { suit: '♥', value: 4, points: 4 } ]
[ { suit: '♥', value: 'K', points: 10 },
  { suit: '♣', value: 6, points: 6 },
  { suit: '♠', value: 'J', points: 10 } ]
0[object Object][object Object][object Object]

I only care about the last 3 objects being logged. The points of 10, 6, 10, which should give me 26.

dealerHand and playerHand are both arrays that have 2 cards pushed into them from the beginning. The dealerHand array will continue to "hit" or add cards if the total point value is less than 17.

After it adds a 3rd card, it logs the 0 and returns arrays of objects.

I believe the reduce method is a good way to get the total within an array, but I'm not sure what I'm missing.

///////////////////////////////////////////////
                CODE
///////////////////////////////////////////////
const dealRandomCard = () => {
     return deckOfCards.splice(Math.floor(Math.random() * 
            deckOfCards.length), 1)[0];
}

// console.log(dealRandomCard());

//////////////////////////////////////////////////////////////
//                DEAL 2 CARDS TO EACH SIDE                 //
//////////////////////////////////////////////////////////////
  for (let i = 0; i < 2; i++) {
     playerHand.push(dealRandomCard());
     dealerHand.push(dealRandomCard());
  }

The dealRandomCard method is just returning one random card from the array that I have defined as deckOfCards with the appropriate suit, value, and points that can be added together to determine a winner.

let playerPoints = 0,
    dealerPoints = 0;

for (let { points } of playerHand) {
    playerPoints += points;
}

for (let { points } of dealerHand) {
    dealerPoints += points
}
    console.log(playerPoints);
    console.log(dealerPoints);

 /////////////////////////////////////////////////////////////

let totalPoints;

if (dealerPoints < 17) {
    dealerHand.push(dealRandomCard());

    totalPoints += points

} else {

    return

}

let totalDealerPoints = 0;

// for (let card of dealerHand) {

 totalDealerPoints += points;

 totalDealerPoints  = dealerHand.reduce((total , amount) => total + 
 amount, 0);
 // }

// return totalDealerPoints
console.log(playerHand);
console.log(dealerHand);
console.log(totalDealerPoints);

///////// REVISED WITH CORRECT WORKING CODE /////////////

 let totalPoints = 0;

 if (dealerPoints < 17) {
    dealerHand.push(dealRandomCard());

   totalPoints += points

 } else {

  return

 }



 // for (let card of dealerHand) {

 totalPoints += points;

 totalPoints  = dealerHand.reduce((total , card) => total + 
 card.points, 0);

Upvotes: 1

Views: 63

Answers (1)

Mark
Mark

Reputation: 92460

It seems like code is missing because in lines like this:

    dealerHand.push(dealRandomCard());
    totalPoints += points

points is not defined anywhere in the current scope and totalPoints was not initialized to zero. But since these values aren't really being used, I'm not sure what to say.

Regarding the reduce, you will get a card with each pass through, so you need to add the card.points:

let dealerHand = [ 
  { suit: '♥', value: 'K', points: 10 },
  { suit: '♣', value: 6, points: 6 },
  { suit: '♠', value: 'J', points: 10 } ]

let totalPoints = dealerHand.reduce((total, card) => total + card.points, 0)
console.log(totalPoints)

Upvotes: 2

Related Questions