본문 바로가기

프로그래머스/JavaScript

[프로그래머스 Javascript] Level2 프린터

function solution(priorities, location) {
    var answer = 0;
    let printList = [];
    let documnet = '';
    let print = false;  
    let max = 0;
    priorities = priorities.map((e, i) => e = {'number': i, 'priority': e});


    while (priorities.length > 0) {
        documnet = priorities.shift();  
        max = priorities.reduce( ( a, c ) =>  a = Math.max(a, c.priority), 0 )        
        print = max > documnet.priority ? false : true;        
        print ? printList.push(documnet) : priorities.push(documnet)
    }


    answer = printList.findIndex(e => e.number === location) + 1
    return answer;
}