(これは、先ほどの質問と似ていますが、同じではないことに注意してください。その質問の解決策は、Math.Random を呼び出すときに括弧を追加することでした)
以下のコードの下部では、ブラックジャックの 2 つのハンドを配り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());