Using native JS to write and wash playing cards

Time:2021-1-14

The card washing process is a classic variable exchange

Steps:

1) From the original array each time traversal, randomly throw a card
2)Random cardAssign toTemporary card
3)First cardAssign toRandom card
4)Temporary cardAssign toEndgame card

 

Implementation code

Var cards = 54; // take a pair of landlords as an example, 13 × 4 ♥♠♦♣  + 2 = 54
function wash(cards) {
    var inputCard = ['A A kind of ', 'A 80 ', 'A A kind of ', 'A Accession ', '2 A kind of ', '2 80 ', '2 A kind of ', '2 Accession ', '3 A kind of ', '3 80 ', '3 A kind of ', '3 Accession ', '4 A kind of ', '4 80 ', '4 A kind of ', '4 Accession ', '5 A kind of ', '5 80 ', '5 A kind of ', '5 Accession ', '6 A kind of ', '6 80 ', '6 A kind of ', '6 Accession ', '7 A kind of ', '7 80 ', '7 A kind of ', '7 Accession ', '8 A kind of ', '8 80 ', '8 A kind of ', '8 Accession ', '9 A kind of ', '9 80 ', '9 A kind of ', '9 Accession ', '10 A kind of ', '10 80 ', '10 A kind of ', '10 Accession ', 'J A kind of ', 'J 80 ', 'J A kind of ', 'J Accession ', 'Q A kind of ', 'Q 80 ', 'Q A kind of ', 'Q Accession ', 'K A kind of ', 'K 80 ', 'K A kind of ', 'K Accession ','Big','small '], // initial and final cards
        Tempcard, // temporary card
        Random card; // random card
    For (VaR J = 0; J < cards; j + +) {// shuffling process
        randomCard = Math.floor(Math.random() * cards);
        tempCard = inputCard[randomCard];
        inputCard[randomCard] = inputCard[j];
        inputCard[j] = tempCard;
    }
    console.log(inputCard);


    /*Check for duplicate random elements*/
    var checkArr = {};
    for (var i = 0; i < cards; i++) {
        Var char = inputcard [i]; // returns the index object array
        if (checkArr[char]) {
            Checkarr [char] + +; // add 1 to the cumulative number of occurrences, and "object [key] = 1" assigns a value to each traversed object attribute
        } else {
            Checkarr [char] = 1; // if it appears for the first time, the number is 1
        }
    }
    console.log (checkarr); // output a complete object, recording each character and its occurrence times
}
wash(cards);

 

Result output

The cards have been shuffled once

Check for duplication