Skip to content
Prev Previous commit
Next Next commit
Add 나누어-떨어지는-숫자-배열.js
  • Loading branch information
chaerin-dev committed Apr 15, 2022
commit ee60a08d2a59c98c4774e9338b36b58bb5b7a82d
9 changes: 9 additions & 0 deletions level-1/나누어-떨어지는-숫자-배열.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ function solution(arr, divisor) {
answer.length === 0 ? answer.push(-1) : null
answer.sort((a, b) => a - b)
return answer;
}

//정답 3. chaerin-dev
function solution(arr, divisor) {
let answer = [];
arr.forEach((e) => {
if (e % divisor === 0) answer.push(e);
});
Copy link
Owner

Choose a reason for hiding this comment

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

24-27 라인이 answer = arr.filter(e => e %divisor === 0) 와 같은 동작을 하지 않을까 의견남겨봅니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그러네요!! filter 메서드가 익숙하지 않아서 떠올리지 못했는데, filter를 사용하면 가독성도 좋고 훨씬 간결한 코드가 되겠네요. 감사합니다!! 👍👍👍

return answer.length ? answer.sort((a, b) => a - b) : [-1];
}