评分卡对象的手

时间:2012-02-10 16:45:47

标签: javascript

// function scoreHandArray scores your hand
function scoreHandArray(hand) {
    var score = 0;
    for (i=0,i<hand.length,i++) {
        score = score + hand[i].value;
    };
    return score;
};

console.log( "You have the " + player[0].face + " of " + player[0].suit " and the " + player[1].face " of " + player[1].suit " for a score of " scoreHandArray(player));

你好,这是我了!这里的函数有一个我找不到的错误,返回SyntaxError: Expected ';'。 (我知道它是函数,因为注释掉console.log没有任何改变。)函数scoreHandArray获取一个对象数组并返回对象的分数。这里有完整的源代码:

// This code defines the Object constructor Card, used to make the card objects
var Card = function(card) {
    this.face = theFace(card);
    this.suit = theSuit(card);
    this.value = theValue(card);
};

// This code creates the Deck to be used.
var deck = [];
for ( i=0 ; i<52 ; i++ ) {
    deck.push( i );
};
for ( i=51 ; i>0 ; i-- ) {
    var random = Math.floor(Math.random()*i);
    var temp = deck[random];
    deck[random] = deck[i];
    deck[i] = temp;
};
// 0-12 is Spades.
// 13-25 is Hearts.
// 26-38 is Clubs.
// 39-51 is Diamonds.

// Now we create the hand of the player and dealer
var player = [];
var dealer = [];

// Now to deal a card to player
player.push(deck.pop());
dealer.push(deck.pop());

// and another
player.push(deck.pop());
dealer.push(deck.pop());

// function theFace gives the face of a card
function theFace( card ) {
    var faces = ["King","Ace","2","3","4","5","6","7","8","9","10","Queen","Jack"];
    return(faces[card%13]);
};

// function theValue uses 'switch' to determine points from a card
function theValue(card) {
    var value = card % 13;
    switch( value ) {

        case(0):
        case(11):
        case(12):
            value = 10;
            break;

        case(1):
            value = 11;
            break;

        default:
            value = value;
            break;

    };
    return value;
};

// function theSuit returns the suit of a card
function theSuit(card) {
    var suit;
    if(card>38) {
        suit = "Diamonds";
    }else if(card>25) {
        suit = "Clubs";
    }else if(card>12) {
        suit = "Hearts";
    }else {
        suit = "Spades";
    };
    return suit;
};

// function toObject the first (numbered) card of of a hand 
// and turns it into an Object with the desired properties
function toObject( hand ) {
    var card = hand.pop();
    if (typeof(card) !== "number") {
        hand.push(card);
    } else {
        var card = new Card (card);
        hand.unshift(card);
    };
    return hand;
};

toObject(player);
toObject(player);
toObject(dealer);
toObject(dealer);

// function scoreHandArray scores your hand
function scoreHandArray(hand) {
    var score = 0;
    for (i=0,i<hand.length,i++) {
        score = score + hand[i].value;
    };
    return score;
};

console.log( "You have the " + player[0].face + " of " + player[0].suit " and the " + player[1].face " of " + player[1].suit " for a score of " scoreHandArray(player));

奇怪,现在用[{1}}循环替换,循环中的for后,它现在响应;。它可能需要ReferenceError: expected ')'? (错误似乎出现在最后一行)行,因为对其进行评论会使错误消失。)我计算在那行中我有2 console.log和2 (

2 个答案:

答案 0 :(得分:1)

看起来你的问题就在这里:

for (i=0,i<hand.length,i++) {
    score = score + hand[i].value;
};

您需要使用for替换;循环中的逗号。此外,}之后的分号也不是必需的。

此外,首选在循环开头使用var i=0,并且还可以防止因引用全局i变量而导致的范围问题。

答案 1 :(得分:0)

您最好的选择是将所有JavaScript代码放在JSLint中。这将帮助您清理可能会出现问题的语法问题,并帮助您节省大量的跨浏览器兼容性。