Question

I am trying to create a deck of cards without using the for loop. I get the error "Uncaught SyntaxError: Unexpected token for line 189 " line 189 being the line with "createcards"

var deck = {
suit: suit = ["Diamond", "Heart", "Club", "Spade"],
name: name = ["Nine", "Ten", "Jack", "Queen", "King", "Ace"],
cards: cards = [],
createcards: for (var i = 0; i < suit.length; i++){
        for (var j = 0; j < name.length; j++){
        deck.cards.push(card(name[j], suit[i]))
    }
},

draw: function (player){
    randnumber = Math.floor((Math.random() * cards.length))
    player.push[cards[randnumber]];

}
};
Était-ce utile?

La solution

Set cretaecards as an anonymous function:

createcards: function(){
    for (var i = 0; i < deck.suit.length; i++){
            for (var j = 0; j < deck.name.length; j++){
            deck.cards.push(card(deck.name[j], deck.suit[i]))
        }
    }
},

Autres conseils

You must define a function in the literal object.

var deck = {
suit: suit = ["Diamond", "Heart", "Club", "Spade"],
name: name = ["Nine", "Ten", "Jack", "Queen", "King", "Ace"],
cards: cards = [],
createcards: function() {
     for (var i = 0; i < suit.length; i++) {
        for (var j = 0; j < name.length; j++) {
           deck.cards.push(card(name[j], suit[i]))
        }
     }
 }
},

You should assign createcards to an anonymous function like so:

createcards: function () {
    for (var i = 0; i < suit.length; i++) {
        for (var j = 0; j < name.length; j++) {
            deck.cards.push(card(name[j], suit[i]));
        }
    }
}

Use a function, like this:

var deck = {
suit: suit = ["Diamond", "Heart", "Club", "Spade"],
name: name = ["Nine", "Ten", "Jack", "Queen", "King", "Ace"],
cards: cards = [],
createcards: 
    function() {
        for (var i = 0; i < suit.length; i++){
            for (var j = 0; j < name.length; j++){
                deck.cards.push(card(name[j], suit[i]))
            }
        } 
    },
draw: function (player){
    randnumber = Math.floor((Math.random() * cards.length))
    player.push[cards[randnumber]];

}

};

You can call the function inside the Object like this:

deck.createcards();

But I suggest that you look at the line with:

deck.cards.push(card(name[j], suit[i]))

because there is no function card here...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top