function solution(priorities, location) {
const waitingList = priorities.map((priority, index) => {
return {
priority,
isTarget: index === location
}
})
let count = 0
while (true) {
let firstDocument = waitingList.shift()
let flag = false
for (let i = 0; i < waitingList.length; i++) {
if (firstDocument.priority < waitingList[i].priority) {
waitingList.push(firstDocument)
flag = true
break
}
}
if (!flag) {
count += 1
if (firstDocument.isTarget) break
}
}
return count;
}