0

I'm currently having a problem on javascript. please help me on how can i shuffle this one within an array. i would really appreciate any help. Thank you so much!

    // ______________________________________________Function to declare our var -> deck of cards

function getDeck() {

    var deck = [
        {suit: "H", face: "A"},
        {suit: "H", face: "2"},
        {suit: "H", face: "3"},
        {suit: "H", face: "4"},
        {suit: "H", face: "5"},
        {suit: "H", face: "6"},
        {suit: "H", face: "7"},
        {suit: "H", face: "8"},
        {suit: "H", face: "9"},
        {suit: "H", face: "10"},
        {suit: "H", face: "J"},
        {suit: "H", face: "Q"},
        {suit: "H", face: "K"},
        {suit: "C", face: "A"},
        {suit: "C", face: "2"},
        {suit: "C", face: "3"},
        {suit: "C", face: "4"},
        {suit: "C", face: "5"},
        {suit: "C", face: "6"},
        {suit: "C", face: "7"},
        {suit: "C", face: "8"},
        {suit: "C", face: "9"},
        {suit: "C", face: "10"},
        {suit: "C", face: "J"},
        {suit: "C", face: "Q"},
        {suit: "C", face: "K"},
        {suit: "D", face: "A"},
        {suit: "D", face: "2"},
        {suit: "D", face: "3"},
        {suit: "D", face: "4"},
        {suit: "D", face: "5"},
        {suit: "D", face: "6"},
        {suit: "D", face: "7"},
        {suit: "D", face: "8"},
        {suit: "D", face: "9"},
        {suit: "D", face: "10"},
        {suit: "D", face: "J"},
        {suit: "D", face: "Q"},
        {suit: "D", face: "K"},
        {suit: "S", face: "A"},
        {suit: "S", face: "2"},
        {suit: "S", face: "3"},
        {suit: "S", face: "4"},
        {suit: "S", face: "5"},
        {suit: "S", face: "6"},
        {suit: "S", face: "7"},
        {suit: "S", face: "8"},
        {suit: "S", face: "9"},
        {suit: "S", face: "10"},
        {suit: "S", face: "J"},
        {suit: "S", face: "Q"},
        {suit: "S", face: "K"},
    ];

    return deck;
}

This is the shuffle codes. i don't know if this is correct so kindly please check also. Thank you!

// ______________________________________________Function to shuffle cards

function shuffle(o) { // o is passed-in array

     for (var j, x, i = o.length; i; j = parseInt(Math.random() * i, 10), x = o[--i], o[i] = o[j], o[j] = x);
     return o;

};


// ______________________________________________Function to retrieve our deck of cards & save into localStorage

function getShoe(decks) {

    var shoe = [];
    var deck = getDeck();
    this.decks = decks;

        for (z=1;z<=decks;z++) {
            shoe.push(deck);
        }

    shoe = shuffle(shoe);
    return shoe;


    localStorage.setItem('shoe', JSON.stringify(shoe));

}
1
  • 1
    What the hell is happening in that for loop? Commented Nov 7, 2013 at 1:52

1 Answer 1

1

What you are doing here is:

var shoe = [];          // create an empty array
var deck = getDeck();   // create a SINGLE deck
this.decks = decks;

    for (z=1;z<=decks;z++) {
        shoe.push(deck);  // insert the SAME deck many times into the array
    }

shoe = shuffle(shoe); // you shuffle the array that contains the same deck many times, not the decks
return shoe;

You can do:

var shoe = [];
for (var i=0; i<decks; i++){
    shoe.push(shuffle(getDeck()));
}
return shoe;
Sign up to request clarification or add additional context in comments.

2 Comments

not shuffling yet var shoe = []; var deck = getDeck(); for (var i=0; i<decks; i++) { shoe.push(shuffle(deck)); } return shoe; i tried yours too var shoe = []; for (var i=0; i<decks; i++){ shoe.push(shuffle(getDeck())); } return shoe; i think b/c after i try to shuffle i save into local storage and the output from local storage is still a non shuffled deck
i just tried it and it worked. jsfiddle.net/GzwJa you can open the JS console and see that each deck inside the shoe is different

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.