为什么这段代码会返回NaN?

时间:2012-08-29 23:11:09

标签: javascript

二十一点游戏:我在下面做了交易功能,它应该返回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));

7 个答案:

答案 0 :(得分: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);
}; 

答案 1 :(得分:1)

你不应该打电话:

console.log(card1.getSuit());

答案 2 :(得分:1)

你有一些复合问题,但你获得NaN的原因是你使用了随机函数。在调用方法/函数后,请包含()

对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());

答案 3 :(得分:0)

变化:

var num = num; 
var suit = suit;

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

答案 4 :(得分: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());

答案 5 :(得分:0)

您需要修复三件事:

  1. 您需要Math.random()上的parens。
  2. 您需要像getSuit()这样调用card1.getSuit(),而不是如何做到这一点。
  3. 您需要实际使getSuitgetNumbergetValue成为Card对象的方法。正如您已声明的那样,它们只是Card构造函数中的本地函数,并且无法从该构造函数外部调用。
  4. 制作这些函数方法有两种常用方法。一种是将它们分配给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());
    

答案 6 :(得分: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;
    };
}