[tkddbs587] 25.01.02#6
Conversation
oh-chaeyeon
left a comment
There was a problem hiding this comment.
전체적으로 가독성 좋은 간결한 코드로 많이 배우고 갑니다~~ 수고하셨습니다😃
| for (let i = 0; i < arr1.length; i++) { | ||
| let newRow = []; | ||
|
|
||
| for (let j = 0; j < arr1[i].length; j++) { | ||
| newRow.push(arr1[i][j] + arr2[i][j]); | ||
| } | ||
| answer.push(newRow); | ||
| } |
There was a problem hiding this comment.
지금도 잘 작동하지만, 가독성면에서 조금 떨어지는 이중 for문을 더 간결하게 할수 있는지 생각을 해봤는데, 이렇게 arr1.entries()과 push메서드를 사용하는 방법은 어떤가 생각해 봤습니다.
for (const [i, row] of arr1.entries()) {
answer.push(row.map((value, j) => value + arr2[i][j]));
}
There was a problem hiding this comment.
와우,, 이렇게까지 해주시다니 코테 왕초보라 모르는 메서드들 너무 많아요 감사합니다 채연님!
There was a problem hiding this comment.
filter메서드랑 sort메서드 사용해서 간단하게 처리하신것 같습니다👍👍
다만, 더 간결하게 코드를 하고 싶다면, 이런식으로도 하셔도 좋을것 같습니다:)
function solution(arr, divisor) {
const result = arr.filter((el) => el % divisor === 0).sort((a, b) => a - b);
return result.length ? result : [-1];
}
근데,성능테스트했을때는 딱히 차이는 안나긴 했습니다...
There was a problem hiding this comment.
오 sort() 까지 한곳에서 해결하고 삼항 연산자로 하니까 훨씬 간결해졌네요! 👍🏻
| for (let i = 0; i < arr.length; i++) { | ||
| if (arr[i] === minNum) { | ||
| arr.splice(i, 1); | ||
| if (arr.length === 0) { | ||
| arr.push(-1); | ||
| } | ||
| } | ||
| } | ||
| return arr; |
There was a problem hiding this comment.
지금도 좋지만, for문 내부에서 splice메서드를 사용하면 배열의 길이가 변해서 불필요한 계산이 발생할 수 있다고 생각합니다... filter를 사용하면 배열을 다시 생성하면서 최소값만 제외할 수 있어서, 이떄는 filter를 사용하는걸 추천합니다👍
const filteredArr = arr.filter((num) => num !== minNum);
return filteredArr.length ? filteredArr : [-1];
There was a problem hiding this comment.
for문에서 splice 쓰면 정말 그렇겠네요 이런 방법이! 감사합니다 🤩
* Array slicing / 기초 * Length of array elements / 기초 * Rotate array / 기초 * Divisible number array / 중급 * Remove_smallest_number / 중급 * Matrix addition / 중급
[배열]
📌 푼 문제
📝 간단한 풀이 과정