프로그래머스 Level#1_완주하지못한 선수

juyeong-s·2021년 6월 22일
0

프로그래머스

목록 보기
2/5

내가 쓴 CODE

function solution(participant, completion) {
    var answer = '';
    var i, j;
    var check;
    for(i=0; i<participant.length; i++){
        check = completion.indexOf(participant[i]);
        if(check === -1){
            answer = participant[i];
        }
        else{
            
        }
    }
    return answer;
}

느낀점

sort함수가 존재한다.
배열이름.sort(); 하면 크기가 작은 순으로 자동정렬된다.
['b', 'c', 'a'] --> ['a', 'b', 'c']

수정한 CODE

function solution(participant, completion) {
    participant.sort();
    completion.sort();
    for (let i=0; i<participant.length; i++){
        if(participant[i]!==completion[i]){
            return participant[i]
        }
    }
}
profile
frontend developer

0개의 댓글