[英]Javascript - how to add points from one array to another - so I can tally them up and compare the two
I am trying to convert the value of the 2 cards in each Hand array to points, so that I can tally them up and compare the two. 我试图将每个手形阵列中2张卡的值转换为点,以便我可以对它们进行计数并比较两者。
Currently, it says my playerPoints is NaN, so my issue is in the for-of loop or my playerHand.point. 当前,它说我的playerPoints是NaN,所以我的问题是在for-of循环或我的playerHand.point中。
for (let i = 0; i < 2; i++) {
playerHand.push(dealRandomCard());
dealerHand.push(dealRandomCard());
}
// console.log(playerHand);
// console.log(dealerHand);
let playerPoints = 0,
dealerPoints = 0;
for (point of playerHand) {
playerPoints += playerHand.point;
}
console.log(playerPoints);
REVISED - WORKING CODE - Thanks to Nina 修订版-工作代码-感谢Nina
for (let { points } of playerHand {
playerPoints += points
}
Assuming the objects of a last question of OP of the same kind, it should be points
in stead of point
. 假设OP的最后一个问题的对象是同类的,它应该是
points
而不是point
。
Beside this, you need to take the points
property by using a destructuring assignment 除此之外,您还需要使用解构分配来获取
points
属性
for (let { points } of playerHand) {
playerPoints += points;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.