0

二十一点游戏:我做了下面的发牌函数,它应该返回一个代表花色的 1-4 之间的随机数,以及另一个代表卡号的 1-3 之间的随机数。

当我通过调用它来测试代码时,console.log(getSuit(card1));它返回 NaN

有谁知道为什么?

// Make your card constructor again here, but make sure to use private
// variables!
function Card(num, suit){
    var num = num; 
    var suit = suit;
    getSuit = function(){
        return suit; 
    }; 
    getNumber = function(){
        return num; 
    };

    getValue = function(card){
        if (card > 10){
            return 10; 
        }else if (card === 1){
            return 11; 
        }else{
            return card; 
        }

    };

}

// 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 card1 = deal();
var card2 = deal();

console.log(getSuit(card1));
4

7 回答 7

4

只需在随机后添加 ()

var deal = function() {
    var suit = Math.floor(Math.random() * 4 + 1);
    var number = Math.floor(Math.random() * 13 + 1);
    return new Card(number, suit);
}; 
于 2012-08-29T23:18:47.630 回答
1

你不应该打电话:

console.log(card1.getSuit());

?

于 2012-08-29T23:15:17.510 回答
1

您遇到了一些复杂的问题,但是您获得 NaN 的原因是您使用了 random 函数。请包括()对方法/函数的所有调用。

对 Card 类进行了其他更改。this.将变量分配给该类实例。同样适用于功能。所以这改变了你调用的方式getSuit(),作为方法调用。它以前工作过,因为您的 getSuit() 被放入全局命名空间。

function Card(num, suit){
    this.num = num; 
    this.suit = suit;
    this.getSuit = function(){
        return suit; 
    }; 
    this.getNumber = function(){
        return num; 
    };

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

    };

}

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 card1 = deal();
var card2 = deal();

console.log(card1.getSuit());
于 2012-08-29T23:20:27.937 回答
0

改变:

var num = num; 
var suit = suit;

this.num = num; 
this.suit = suit;

等等

于 2012-08-29T23:13:39.980 回答
0

试试这个:

// Make your card constructor again here, but make sure to use private 
// variables!
function Card(num, suit){
    // NOTE: "this."
    this.num = num;
    this.suit = suit;
    this.getSuit = function(){
        return this.suit;
    };
    this.getNumber = function(){
        return this.num;
    };

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

// 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(){
    // NOTE: Math.random()
    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 card1 = deal();
var card2 = deal();

// NOTE: card1.getSuit()
console.log(card1.getSuit());
于 2012-08-29T23:21:50.307 回答
0

您需要解决三个不同的问题:

  1. 你需要在Math.random().
  2. 你需要getSuit()像这样打电话card1.getSuit()而不是你怎么做的。
  3. 您需要真正地制作getSuitgetNumbergetValue成为 Card 对象的方法。正如您到目前为止所声明的那样,它们只是 Card 构造函数内部的局部函数,不能从该构造函数外部调用。

有两种常用的方法来制作这些函数方法。一种是将它们分配给 Card 原型对象。另一种是在构造函数中分配它们this

以下是将它们分配给原型对象的样子:

function Card(num, suit){
    this.num = num; 
    this.suit = suit;
}

Card.prototype = {
     getSuit: function() {
         return suit;
     },
     getNumber: function() {
         return num;
     },
     getValue: function(card) {
        if (card > 10){
            return 10; 
        }else if (card === 1){
            return 11; 
        }else{
            return card; 
        }
    }
};

// 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 card1 = deal();
var card2 = deal();

console.log(card1.getSuit());

如果您想保留私有变量numsuit那么您必须像这样在 Card 内部定义方法:

// Make your card constructor again here, but make sure to use private
// variables!
function Card(num, suit){
    var num = num; 
    var suit = suit;
    this.getSuit = function(){
        return suit; 
    }; 
    this.getNumber = function(){
        return num; 
    };

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

    };

}

// 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 card1 = deal();
var card2 = deal();

console.log(card1.getSuit());
于 2012-08-29T23:21:54.227 回答
0

这个版本保留了私有变量的使用。请注意,函数参数自动为私有变量,因此您无需重新声明它们。

function Card(num, suit) {
    this.getSuit = function() {
        return suit;
    }; 
    this.getNumber = function() {
        return num;
    };
    this.getValue = function() {
        if (num > 10)
            return 10;
        if (num == 1)
            return 11;
        return num;
    };
}
于 2012-08-29T23:28:42.660 回答