0

I have the following 2 functions:

function undo(){
    var card = discardPile.pop();
    if( card.col != -1 ){
        sendToCol( card );
    }else{
        sendToDrawPile( card );
    }
    cardsLeft();
}

function undowaste(){
    var card = wastePile.pop();
    if( card.col != -1 ){
        sendToCol( card );
    }else{
        sendToDrawPile( card );
    }
    cardsLeft();
}

They are the same except for the card value. So I was wondering, can I merge these 2? And how to do this?

EDIT: I use this function to execute both:

function restart(){
    if( discardPile.length){
        undo();
        setTimeout(restart,75);
    }if( wastePile.length){
        undowaste();
        setTimeout(restart,75);
    }
}

Thanks

1 Answer 1

4
function undo(pile){
    var card = pile.pop();
    if( card.col != -1 ){
        sendToCol( card );
    }else{
        sendToDrawPile( card );
    }
    cardsLeft();
}

undo(wastePile);
undo(discardPile);

or

function undo(card){
    if( card.col != -1 ){
        sendToCol( card );
    }else{
        sendToDrawPile( card );
    }
    cardsLeft();
}

undo(wastePile.pop());
undo(discardPile.pop());

To run at intervals

setInterval(function() {
  if(wastePile.length>0) undo(wastePile.pop());
  if(discardPile.length>0) undo(discardPile.pop());
},200);
Sign up to request clarification or add additional context in comments.

1 Comment

Looks good, but I can't seem to get it working. I've added an extra function to the code. I use this function to call the other 2 functions, but it doesn't seem to work after "merging" them.

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.