
一、题目描述💯
- 有一堆扑克牌,将牌堆第一张放到桌子上,再将接下来的牌堆的第一张放到牌底,如此往复;
最后桌子上的牌顺序为: (牌底) 1,2,3,4,5,6,7,8,9,10,11,12,13 (牌顶);
- 问:原来那堆牌的顺序,用函数实现
二、代码实现🌂
function sortPuke(arr) {
let keyList = [];
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]));
感谢🙇
点赞支持👍