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 }
source
share