Javascript card game, constructor function error

I create a basic card game (21) in JavaScript, where each player has a Card Hand. Players start with 2 cards, but I want to add new cards depending on the total cost of the cards and configure whether there is an ace present or not.

Unfortunately, I get this error: FATAL ERROR: CALL_AND_RETRY_LAST Failed to allocate - JavaScript heap from memory. I think the problem is with this this.newCard method, I just can not understand why it does not work.

function Hand () {
    this.cards = []
    this.cardsAtStart = function() {
        this.cards.push(new DealCards, new DealCards)
        return this.cards
    }
    this.checkValue = function () {
        let cardsInHand = this.cardsAtStart()

        let ace = false
        let value = 0
        for (let i = 0; i < cardsInHand.length; i++) {
            if (cardsInHand[i].cardnumber === 'A' && !ace) {
                ace = true
                value = value + 13
            }
            value = value + cardsInHand[i].cardvalue
        }
        if (ace && value  > 21) {
            value = value - 13
        }
        console.log(value)

        return value
    }
    this.newCard = function () {
        let value = this.checkValue()
        console.log(value)
        while (value < 15) {
            this.cards.push(new DealCards)
        }
        if (value > 15) {
            endGame()
        }
        console.log(this.cards)
    }
}

The map object is as follows: { suit: '♦', cardnumber: 'K', cardvalue: 13 }

+4
source share
4 answers

You do not check the value of each cycle:

this.newCard = function () {
    let value = this.checkValue()
    console.log(value)
    while (value < 15) {
        this.cards.push(new DealCards);
        // check value again here!
        value = this.checkValue()
    }
    if (value > 15) {
        endGame()
    }
    console.log(this.cards)
}

But also change checkValue to display the AtStart maps from there and into the constructor.

, , :

while (value < 15) {
  this.cards.push(new DealCards);
  value = this.cards.reduce((all, current) => all + current.cardvalue, 0),
}

a >

, Ace . . AtStart . checkValue, .

+2

while, value :

while (value < 15) {
   this.cards.push(new DealCards)
}

value while, false.

- :

while (value < 15) {
    this.cards.push(new DealCards);
    value++;
}
+2

, ,

 while (value < 15) {
     this.cards.push(new DealCards)
 }

, 15, , .

, , 15, - , - ,

if (value < 15) {
       this.cards.push(new DealCards)
 }

So, to fix your problem, you will need to update the value each time it is completed by calling your function this.checkValue().

while (value < 15) {
     this.cards.push(new DealCards);
     value = this.checkValue();
 }
0
source

This loop cycle will never end

while (value < 15) {
            this.cards.push(new DealCards)
        }

You will need to print the value of the ++ or ++ value so that it ends.

while (value < 15) {
            this.cards.push(new DealCards);
            value++;
        }
0
source

Source: https://habr.com/ru/post/1691529/


All Articles