Не работает код на Vue
Всем привет, есть код для игры в BlackJack(21) Скрипт ниже должен выводить рандомное значение в playerPoints: 0, dealerPoints: 0.
Но у меня выходит ошибка Cannot read property 'slice' of undefined" в функции checkHandValue(hand)
Подскажите, в чем может быть ошибка?
el: '#app',
data: {
isGameRunning: false,
playerPoints: 0,
dealerPoints: 0,
playerHand: [],
dealerHand: [],
deck: [],
cardSwitch: []
},
beforeMoment () {
this.deck = this.generateCardPool();
this.cardSwitch = new Array(52).fill(false);
},
methods: {
startNewGame() {
this.isGameRunning = true;
for(let i = 0; i < 2; i++) {
this.playerHand.push(this.dealCard())
this.dealerHand.push(this.dealCard())
console.log(this.playerHand)
}
this.playerPoints = this.checkHandValue(this.playerHand);
this.dealerPoints = this.checkHandValue(this.dealerHand);
},
generateCardPool () {
const colors = ['S', 'H', 'D', 'C'];
const values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'Q', 'K'];
const pool = [];
values.map(v => {
colors.map(c => {
pool.push(`${v}${c}`)
});
});
return pool;
},
checkHandValue(hand) {
let value = 0;
hand.map(e => {
let v = e.slice(0, -1);
if(Number.isNaN(parseInt(v)) && hand.length == 2 && v === 'A') {
v = 11;
} else if (Number.isNaN(parseInt(v)) && hand.length >= 3 && v == 'A') {
v = 1
} else if (Number.isNaN(parseInt(v))) {
v = 10
} else {
v = parseInt(v)
}
value += v
});
return value;
},
dealCard () {
let index;
do {
index = Math.floor(Math.random() * 52);
} while(this.cardSwitch[index]);
const card = this.deck[index];
this.cardSwitch[index] = true;
console.log(card)
return card;
}
}
})