0

(これは、先ほどの質問と似ていますが、同じではないことに注意してください。その質問の解決策は、Math.Random を呼び出すときに括弧を追加することでした)

以下のコードの下部では、ブラックジャックの 2 つのハンドを配りmyhandyourhandそのハンドをコンソールに記録しています。

"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());
4

3 に答える 3

2

あなたのgetValue機能は間違っています。そのはず:

this.getValue = function() {
  if( this.number>10) return 10;
  if( this.number==1) return 11;
  return this.number;
}

何かが間違っていたというヒントは、引数を指定しthis.card1.getValue()て定義this.getValue(number)したのに、引数なしで呼び出していることです。

于 2012-08-29T23:57:13.613 に答える
1

値を取得する関数はnumber引数 this.getValue = function(number)を受け入れます

しかし、ここでは値を渡していません:

var score1 = this.card1.getValue();
var score2 = this.card2.getValue();
于 2012-08-30T00:07:22.700 に答える
1

card.getValue() に対処するときは、いくつかの入力が必要です

this.getValue = function(number){
    if (number > 10){
        return 10; 
    }else if (number === 1){
        return 11; 
    }else{
        return number; 
    }

};

関数は何も返さないため、NaN になります。これを解決するには、代わりに this.number を使用してください

于 2012-08-30T00:00:38.510 に答える