(请注意,这与我刚才提出的问题相似但不同 - 该问题的解决方案是在调用 Math.Random 时添加括号)
在下面代码的底部,我处理了两手二十一点myhand
,yourhand
然后将手记录到控制台
"I scored a "+myHand.score()+" and you scored a "+ yourHand.score());
但是,我得到的结果是
I scored NaN and you scored a NaN
最初,Card 构造函数中的 getValue 方法传递了一个名为的参数,card
但构建 Hand 构造函数的说明说调用 getValue 而不传递参数
this.card1.getValue();
所以我将 getValue 方法更改为采用var number
(在 Card 构造函数中)
无论如何,长话短说,无论我做什么,它都会打印出来
I scored NaN and you scored a NaN
而且我不确定我到底哪里出错了。
// Make your card constructor again here, but make sure to use private
// variables!
function Card(num, suit){
var number = num;
var suits = suit;
this.getSuit = function(){
return suits;
};
this.getNumber = function(){
return number;
};
this.getValue = function(number){
if (number > 10){
return 10;
}else if (number === 1){
return 11;
}else{
return number;
}
};
}
function Hand(){
this.card1 = deal();
this.card2 = deal();
this.score = function(){
var score1 = this.card1.getValue();
var score2 = this.card2.getValue();
return score1 + score2;
};
}
// Make a deal function here. It should return a new card with a suit
// that is a random number from 1 to 4, and a number that is a random
// number between 1 and 13
var deal = function(){
var suit = Math.floor(Math.random() * 4 + 1);
var number = Math.floor(Math.random() * 13 + 1);
return new Card(number, suit);
};
// examples of the deal function in action
var myHand = new Hand();
var yourHand = new Hand();
console.log("I scored a "+myHand.score()+" and you scored a "+ yourHand.score());