0

I'm creating a matching game and I'm trying to add a class from an array to match against. The code I have below creates the classes I need, then randomizes them.

My problem is in the randomizeDeck function. I'm trying to add each of the classes to the specified element twice. When I console.log the code the classes gets added to the first six elements but not the last six, which I need it to do so that I have the classes to match against in the matching game I'm creating.

var cardDeck = new Array();

function createDeck() {
    for (i = 1; i <= 6; i++) {
        cardDeck.push("card-" + i);
    }
}
createDeck();

var randDeck = cardDeck.sort(randOrd);

function randomizeDeck() {
    card.each(function(i){
        $(this).addClass(randDeck[i]);
    });
}
randomizeDeck();
3
  • what is card in "card.each"? what this variable have? Commented Sep 26, 2013 at 18:50
  • The variable card is a div with the class card. That div is replicated 12 times. Commented Sep 26, 2013 at 18:52
  • andi's solution is probably your best way to go. Commented Sep 26, 2013 at 19:26

4 Answers 4

1

I think your createDeck function needs to create 12 classes instead of 6. Just push each one twice:

function createDeck() {
    for (i = 1; i <= 6; i++) {
        cardDeck.push("card-" + i);
        cardDeck.push("card-" + i);
    }
}

Then you'll have an array of 12 classes (2 each of 6 unique classes), which will be randomized and assigned to the 12 cards.

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

3 Comments

Don't forget the randomized sort that happens on her initial array.
@BumbleB2na - That sort will work fine with this. Do you see an issue?
actually no.. now that you mention it that would mix them up better than in my solution
0

I suggest a separate variable to keep track of the index, rather that the each index. Once you've gone through the pack once, it might be a good idea to shuffle the deck again so the order is different on the second pass. YMMV.

function sortCards(randOrd) {
  randDeck = cardDeck.sort(randOrd);
}

function randomizeDeck() {
  var count = 0;
  cards.each(function(i) {
    if (i === 6) { count = 0; sortCards(randOrd); }
    $(this).addClass(randDeck[count]);
    count++;
  });
}

Comments

0

Your randomizeDeck() function can be rewritten to use the same array of class names twice:

function randomizeDeck() {
    card.each(function(i){
        if(i < 6)
            $(this).addClass(randDeck[i])
        else
            $(this).addClass(randDeck[i-6]);
    });
}

Note: I would rewrite the variable card as $cards so that you know it's a jQuery object and in this case a collection of them. Otherwise, its hard to tell it apart from any other javascript var.

4 Comments

That is correct, there are a dozen cards that I'm trying to add a class that is duplicated for matching purposes. e.g. card-1 that has a match of card-1 etc.
Then you're going outside the bounds of your array
Any suggestion on how I could apply the same array to the entire block of elements?
Sure but I'm still only guessing with my answer. Some of your code is hidden, such as assigning the value of card and seeing more of your code would help.
0

Try something like this - it's tested now updated SEE THIS FIDDLE http://jsfiddle.net/8XBM2/1/

var cardDeck = new Array();

function createDeck() {
    for (i = 1; i <= 6; i++) {
        cardDeck.push("card-" + i);
    }
}
createDeck();
var randDeck = cardDeck.sort();
alert(randDeck);

function randomizeDeck() {
    var x = 0;
    $('div').each(function(i){
     if ( i > 5) { 

                $(this).addClass(randDeck[x]);
         x++; 
      } else {
        $(this).addClass(randDeck[i]);
    }
    });
}
randomizeDeck();

2 Comments

I am assuming you want to restart the iteration again after 6
Yeah, I want the array to be applied twice so that I have a class that I can match two cards against.

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.