강의로 돌아가기
solwoo

5번만 실패

function solution(n, lost, reserve) {
var answer = 0;
var newArr = [];
var sortReserve = reserve.sort();
var sortLost = lost.sort();
answer = n - lost.length;

for(var i = 0; i < sortReserve.length; i++){
if(sortLost.includes(sortReserve[i])){
newArr.push(sortReserve[i])
sortLost.splice(sortLost.indexOf(sortReserve[i]), 1);
}else{
if(sortLost.includes(sortReserve[i]-1)){
newArr.push(sortReserve[i]-1);
sortLost.splice(sortLost.indexOf(sortReserve[i]-1), 1);
}else if(sortLost.includes(sortReserve[i]+1)){
newArr.push(sortReserve[i]+1);
sortLost.splice(sortLost.indexOf(sortReserve[i]+1), 1);
}
}
}
console.log(newArr);

return answer + newArr.length;
}

왜 실패하는 것인지 잘 모르겠습니다.

작성중인 코드―solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function solution(n, lost, reserve) {
    var answer = 0;
    var newArr = [];
    var sortReserve = reserve.sort();
    var sortLost = lost.sort();
    answer = n - lost.length;

    for(var i = 0; i < sortReserve.length; i++){
      if(sortLost.includes(sortReserve[i])){
        newArr.push(sortReserve[i])
        sortLost.splice(sortLost.indexOf(sortReserve[i]), 1);
      }else{
        if(sortLost.includes(sortReserve[i]-1)){
          newArr.push(sortReserve[i]-1);
          sortLost.splice(sortLost.indexOf(sortReserve[i]-1), 1);
        }else if(sortLost.includes(sortReserve[i]+1)){
          newArr.push(sortReserve[i]+1);
          sortLost.splice(sortLost.indexOf(sortReserve[i]+1), 1);
        }
      }
    }
    console.log(newArr);

    return answer + newArr.length;
}
0 개의 답변
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다. 마크다운 가이드 를 참고하세요.