인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

dragoocho's profile image
dragoocho

asked

Introduction to Javascript Algorithm Problem Solving (Coding Test Preparation)

8. Seven Dwarfs

이번 강의의 답은 기본적으로 모든 케이스에 적용되지 않는 것 같습니다.

Written on

·

381

8

function solution(arr) {
  let answer = arr;
  let sum = arr.reduce((ab=> a + b0);

  for (let i = 0i < 8i++) {
    for (let j = i + 1j < 9j++) {
      if (sum - (answer[i] + answer[j]) === 100) {
        answer.splice(j1);
        answer.splice(i1);
      }
    }
  }
  return answer;
}

let arr = [2072319101528813];
console.log(solution(arr));
 
// 이렇게 배열의 요소 중에서 25를 28로 바꾸면 
// [ 7, 19, 10, 8, 13 ] 로 출력이 됩니다. 
// 이런 경우 sum - (answer[i] + answer[j]) === 100 
// 라는 조건을 두 번 만족시켜서 그런 것 같습니다.
 
 
 
 
 
function solution(arr) {
  let answer = arr;
  let sum = arr.reduce((ab=> a + b0);

  endOfCircuitfor (let i = 0i < 8i++) {
    for (let j = i + 1j < 9j++) {
      if (sum - (answer[i] + answer[j]) === 100) {
        answer.splice(j1);
        answer.splice(i1);
        break endOfCircuit;
      }
    }
  }
  return answer;
}

let arr = [2072319101528813];
console.log(solution(arr));

// [ 7, 19, 10, 15, 28, 8, 13]
// 조건을 한번만 찾아도 종료시키는 
// break문을 추가해야 올바른 정답이 아닌가 싶습니다. 
javascript코테 준비 같이 해요!

Answer 1

1

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

그렇네요.  답을 찾으면 break 문을 해줘야 겠네요. 감사합니다.

dragoocho's profile image
dragoocho

asked

Ask a question