Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
share|improve this question
    
what is card in "card.each"? what this variable have? –  Hugo Tostes Sep 26 '13 at 18:50
    
The variable card is a div with the class card. That div is replicated 12 times. –  Johanna Ruiz Sep 26 '13 at 18:52
    
andi's solution is probably your best way to go. –  BumbleB2na Sep 26 '13 at 19:26

4 Answers 4

up vote 0 down vote accepted

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++;
  });
}
share|improve this answer

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();
share|improve this answer
    
I am assuming you want to restart the iteration again after 6 –  James Daly Sep 26 '13 at 19:03
    
Yeah, I want the array to be applied twice so that I have a class that I can match two cards against. –  Johanna Ruiz Sep 26 '13 at 19:08
    
see my updated fiddle –  James Daly Sep 26 '13 at 19:17

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.

share|improve this answer
    
Don't forget the randomized sort that happens on her initial array. –  BumbleB2na Sep 26 '13 at 19:15
    
@BumbleB2na - That sort will work fine with this. Do you see an issue? –  andi Sep 26 '13 at 19:17
    
actually no.. now that you mention it that would mix them up better than in my solution –  BumbleB2na Sep 26 '13 at 19:23

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.

share|improve this answer
    
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. –  Johanna Ruiz Sep 26 '13 at 18:54
    
Then you're going outside the bounds of your array –  BumbleB2na Sep 26 '13 at 18:57
    
Any suggestion on how I could apply the same array to the entire block of elements? –  Johanna Ruiz Sep 26 '13 at 19:01
    
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. –  BumbleB2na Sep 26 '13 at 19:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.