0

This is my code:

var shuffle = function() {
    var set = [];
    var waiter = [];
    var suitset = ["Hearts","Clubs","Diamonds","Spades"];
    for (var r = 1;r <= 13;r++) {
        for (var s = 0;s < suitset.length;s++) {
            card = [];
            var cardeval = evalcard(r); //This is an input from another function
            card.push(cardeval[0]);
            card.push(suitset[s]);
            card.push(cardeval[1]);
            waiter.push(card);
        }
    }
    while (waiter.length) { //This is what randomizes the deck aka shuffles
        set.push(waiter.splice(Math.random() * waiter.length,1));
    }

    return set;
};

I'm trying to build a matrix of cards (I'm using matrix here as an array of arrays). There will be 52 arrays of card arrays, each of which holds a card number, suit, and value. But after I call the function, card returns as a string and not an array, even though it is defined with array brackers []. Can anyone see what I'm doing wrong here?

2 Answers 2

2

Live Demo

function evalcard() { return [1,2]}

    var shuffle = function() {
        var set = [];
        var waiter = [];
        var suitset = ["Hearts","Clubs","Diamonds","Spades"];

        for (var r = 1;r <= 13;r++) {
            for (var s = 0;s < suitset.length;s++) {
                var card = {},
                    cardeval = evalcard(r); 

                card.num = cardeval[0];
                card.suite = suitset[s];
                card.val = cardeval[1];

                waiter.push(card);
            }
        }

        while (waiter.length) { //This is what randomizes the deck aka shuffles
            var randomNum = Math.floor(Math.random() * (waiter.length-1));
            set.push(waiter[randomNum]);
            waiter.splice(randomNum ,1)
        }
        return set;
    };

    var test = shuffle();
    console.log(test);
    alert(test[0].suite);

Made the cards into objects, and just passed them into an array instead just for easier accessibility.

Sign up to request clarification or add additional context in comments.

Comments

0

Works for me

http://jsfiddle.net/mplungjan/LuUT5/

function evalcard() { return [1,2]}
var shuffle = function() {
    var set = [];
    var waiter = [];
    var suitset = ["Hearts","Clubs","Diamonds","Spades"];
    for (var r = 1;r <= 13;r++) {
        for (var s = 0;s < suitset.length;s++) {
            card = [];
            var cardeval = evalcard(r); //This is an input from another function
            waiter.push([cardeval[0],suitset[s],cardeval[1]]);
        }
    }
    while (waiter.length) { //This is what randomizes the deck aka shuffles
        var x = waiter.splice(Math.random() * waiter.length,1);
        set.push(x);
    }
    return set;
};
var deck = shuffle()[0];
alert(deck[0][1])

1 Comment

@mplungjan--no, shuffle()[1] returns (number,suit,value) as a string... if you try calling shuffle()[1][1] it will return undefined. Maybe this is a syntax problem? is matrix()[n][n] the proper way to call arrays within arrays?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.