队列是什么?先进先出,只能在队列一端插入,另一段删除。排队不能插队,只能从队尾排。打印文档有个打印队列,谁先来谁先打印出来。后者必须等待前者结束后才能进行。
js特点是是单线程,引入了异步非阻塞式的事件循环机制。

Psromise - 微任务队列 UI渲染等其他 - 宏任务
每一个宏任务执行完检查有没有微任务,没有了再去执行下一个宏任务
击鼓传花
1、队列版本
function game(list, num){
let queue = new Queue();
for(let i = 0; i < list.length; i++){
queue.enqueue(list[i]);
}
while(queue.size()>1){
for (let j = 0; j < num; j++){
queue.enqueue(queue.dequeue());
}
queue.dequeue();
}
return queue.dequeue();
}
2、通俗易懂版本
function josephus(list, step) {
const arr = [...list];
let index = 0;
while (arr.length > 1) {
// 计算要淘汰的位置
index = (index + step - 1) % arr.length;
// 移除该元素
arr.splice(index, 1);
}
return arr[0];
}