Skip to content
Merged
Show file tree
Hide file tree
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
Empty file removed tkddbs587/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions tkddbs587/Array/Array_slicing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function solution(numbers, num1, num2) {
return numbers.slice(num1, num2 + 1);
}
14 changes: 14 additions & 0 deletions tkddbs587/Array/Matrix_addition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function solution(arr1, arr2) {
let answer = [];

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);
}
Comment on lines +4 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

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

지금도 잘 작동하지만, 가독성면에서 조금 떨어지는 이중 for문을 더 간결하게 할수 있는지 생각을 해봤는데, 이렇게 arr1.entries()push메서드를 사용하는 방법은 어떤가 생각해 봤습니다.

 for (const [i, row] of arr1.entries()) {
  answer.push(row.map((value, j) => value + arr2[i][j]));
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

와우,, 이렇게까지 해주시다니 코테 왕초보라 모르는 메서드들 너무 많아요 감사합니다 채연님!


return answer;
}
12 changes: 12 additions & 0 deletions tkddbs587/Array/Remove_smallest_number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function solution(arr) {
let minNum = Math.min(...arr);
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;
Comment on lines +3 to +11
Copy link
Collaborator

@oh-chaeyeon oh-chaeyeon Jan 6, 2025

Choose a reason for hiding this comment

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

지금도 좋지만, for문 내부에서 splice메서드를 사용하면 배열의 길이가 변해서 불필요한 계산이 발생할 수 있다고 생각합니다... filter를 사용하면 배열을 다시 생성하면서 최소값만 제외할 수 있어서, 이떄는 filter를 사용하는걸 추천합니다👍

const filteredArr = arr.filter((num) => num !== minNum); 
return filteredArr.length ? filteredArr : [-1]; 

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

for문에서 splice 쓰면 정말 그렇겠네요 이런 방법이! 감사합니다 🤩

}
11 changes: 11 additions & 0 deletions tkddbs587/Array/Rotate_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function solution(numbers, direction) {
if (direction === "right") {
const lastElement = numbers.pop();
numbers.unshift(lastElement);
} else if (direction === "left") {
const firstElement = numbers.shift();
numbers.push(firstElement);
}

return numbers;
}
10 changes: 10 additions & 0 deletions tkddbs587/Array/divisible_number_array.js
Copy link
Collaborator

Choose a reason for hiding this comment

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

filter메서드랑 sort메서드 사용해서 간단하게 처리하신것 같습니다👍👍
다만, 더 간결하게 코드를 하고 싶다면, 이런식으로도 하셔도 좋을것 같습니다:)

function solution(arr, divisor) {
  const result = arr.filter((el) => el % divisor === 0).sort((a, b) => a - b);
  return result.length ? result : [-1]; 
}

근데,성능테스트했을때는 딱히 차이는 안나긴 했습니다...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

오 sort() 까지 한곳에서 해결하고 삼항 연산자로 하니까 훨씬 간결해졌네요! 👍🏻

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function solution(arr, divisor) {
let newArr = arr.filter((el) => el % divisor === 0); // 나누어 떨어지는 엘리먼트가 담긴 새 배열 반환

// 나누어 떨어지는 엘리먼트 없으면 그냥 [-1] 반환
if (newArr.length === 0) {
return [-1];
}

return newArr.sort((a, b) => a - b); // 오름차순 정렬
}
3 changes: 3 additions & 0 deletions tkddbs587/Array/duplicate_count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function solution(array, n) {
return array.filter((el) => el === n).length;
}
3 changes: 3 additions & 0 deletions tkddbs587/Array/length_of_array_elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function solution(strlist) {
return strlist.map((str) => str.length);
}
Loading