function solution(priorities, location) {
let isDone = false;
let count = 0;
while (!isDone) {
let max = Math.max(...priorities);
if (priorities[0] === max) {
count++;
if (location === 0) {
isDone = true;
} else {
priorities.shift();
location-=1;
}
} else {
let temp = priorities.shift();
priorities.push(temp);
if (location === 0) {
location = priorities.length - 1;
} else {
location-=1;
}
}
if (priorities[0] === -1) {
isDone = true;
}
}
return count;
}