function solution(priorities, location) {
let arr = [...priorities];
let queue = priorities.map((priority, i) => [priority, i]);
let order = 1;
while (queue.length > 0) {
const max = Math.max(...arr);
const node = queue.shift();
arr.shift();
if (node[0] === max && node[1] === location) return order;
if (node[0] !== max) {
queue.push(node);
arr.push(node[0]);
} else {
order++;
}
}
return 0;
}