Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions level-2/예상-대진표.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,30 @@ function solution(n, a, b) {
}
return currentRound
}

//정답 2 - le2sky
function solution(n, a, b) {
let arr = Array.from({ length: n }, () => 0)
arr[b - 1] = "B"
arr[a - 1] = "A"

const isDiff = () => {
return (
(arr.indexOf("A") + 1 > arr.length / 2 && arr.indexOf("B") + 1 <= arr.length / 2) ||
(arr.indexOf("A") + 1 <= arr.length / 2 && arr.indexOf("B") + 1 > arr.length / 2)) ? true : false
Comment on lines +24 to +26
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌱 반영해도 좋고 넘어가도 좋아요. 🌱

불필요한 tenary operator가 사용되어 가독성이 조금 떨어진다고 생각합니다! boolean 값을 직접 리턴 하시는 것보단 비교문 그 자체를 쓰시는 것이 좋을 것이라 생각됩니다!

Suggested change
return (
(arr.indexOf("A") + 1 > arr.length / 2 && arr.indexOf("B") + 1 <= arr.length / 2) ||
(arr.indexOf("A") + 1 <= arr.length / 2 && arr.indexOf("B") + 1 > arr.length / 2)) ? true : false
return (
(arr.indexOf("A") + 1 > arr.length / 2 && arr.indexOf("B") + 1 <= arr.length / 2) ||
(arr.indexOf("A") + 1 <= arr.length / 2 && arr.indexOf("B") + 1 > arr.length / 2))

};
const isLeft = () => {
return (arr.indexOf("A") + 1 > arr.length / 2) ? false : true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌱 반영해도 좋고 넘어가도 좋아요. 🌱

이전 코멘트와 동일하게 불필요한 boolean 값들을 제거하시면 더 좋을 것이라 생각합니다!

Suggested change
return (arr.indexOf("A") + 1 > arr.length / 2) ? false : true
return arr.indexOf("A") + 1 <= arr.length / 2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 생각은 전혀 못했었네요 항상 좋은 리뷰 감사드립니다! ㅎㅎ

};

//대진표의 절반을 기준으로 양옆에 A와 B가 있을 경우 log2N을 구하면 라운드 수가 나옴
while (!isDiff()) {
if (isLeft()) {
arr.splice(arr.length / 2)
} else {
arr.splice(0, arr.length / 2)
}
Comment on lines +34 to +38
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌱 반영해도 좋고 넘어가도 좋아요. 🌱

위의 다른 코드들 처럼 ternary operator를 사용해보시는 것도 좋을 것 같습니다!

Suggested change
if (isLeft()) {
arr.splice(arr.length / 2)
} else {
arr.splice(0, arr.length / 2)
}
isLeft() ? arr.splice(arr.length / 2) : arr.splice(0, arr.length / 2)

}
return Math.log2(arr.length)
}