Poker problem

topic

There is a pile of playing cards, put the first card of the pile on the table, and then put the first of the following piles on the bottom of the card, and so on;

The order of the cards on the final table is: (bottom card) 1,2,3,4,5,6,7,8,9,10,11,12,13 (top of card);

Question: The order of the original pile of cards is realized by function

Code

function sortPuke(arr) {
    
    
    let keyList = [];//arr对应的key
    let alenList = [];
    let result = [];//最终结果
    let spot = true;
    if (arr.length === 1) {
    
    
        return arr
    }
    if (arr.length > 1) {
    
    
        for (let index = 0; index < arr.length; index++) {
    
    
            alenList.push(index)
        }
        while (alenList.length) {
    
    
            if (alenList.length === 1) {
    
    
                keyList.push(alenList.shift())
            }
            if (spot && alenList.length > 1) {
    
    
                keyList.push(alenList.shift())
            }
            if (!spot && alenList.length > 1) {
    
    
                alenList.push(alenList.shift())
            }
            spot = !spot
        }
    }
    for (let j = 0; j < arr.length; j++) {
    
    
        result[keyList[j]] = arr[j]
    }
    return result;
}
console.log(sortPuke([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]));
//[ 1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7 ]

Soul Chicken Soup

A line in a play: "Leave the sadness and miss until the victory, go to the grave and say, now we still have to complete the higher-level tasks!"

Guess you like

Origin blog.csdn.net/XINpxXIN/article/details/104792457