web123456

Queue of JS data structure

function passGame(nameList, number) { const queue = new Queue(); // Make a queue first for (let name of nameList) { queue.enqueue(name); } console.log("Original array:", queue.items); //['lily'] // Start counting while (queue.size() > 1) { // When it is not a number, rejoin to the end of the team // When it is number, delete it for (let i = 0; i < number - 1; i++) { // That is, the elements deleted from the team are added to the end of the team again queue.enqueue(queue.dequeue()); console.log("Non-number joins the team", queue.items); } queue.dequeue() console.log(`Delete${number}After`, queue.items); } console.log("Final array:", queue.items); //['lily'] // Get the last person left const endName = queue.front(); return nameList.indexOf(endName); } // passGame() test const names = ['lily', 'lucy', 'tom', 'tony', 'jack']; const targetIndex = passGame(names, 4); console.log('The ultimate winner is:', names[targetIndex]); //lily