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
12 changes: 12 additions & 0 deletions unn04012/array/Array_of_divisible_numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function solution(arr, divisor) {
const answer = [];

for (const e of arr) {
if (e % divisor === 0) answer.push(e);
}
if (answer.length === 0) answer.push(-1);

return answer.sort((a, b) => a - b);
}

console.log(solution([5, 9, 7, 10], 5));
31 changes: 31 additions & 0 deletions unn04012/array/Matrix_multiplication.js
Copy link
Collaborator

Choose a reason for hiding this comment

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

저는 많이 헤맨 문제였는데...Array.from을 사용한 배열 초기화한 부분과 reduce활용으로 깔끔하게 잘 구현된 코드인것 같습니다👍👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function solution(arr1, arr2) {
const n = arr1.length;
const m = arr2[0].length;

const answer = Array.from({ length: n }, () => Array(m).fill(0));

for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
const result = arr1[i].reduce((acc, cur, idx) => acc + cur * arr2[idx][j], 0);

answer[i][j] = result;
}
}

return answer;
}

console.log(
solution(
[
[2, 3, 2],
[4, 2, 4],
[3, 1, 4],
],
[
[5, 4, 3],
[2, 4, 1],
[3, 1, 1],
]
)
);
19 changes: 19 additions & 0 deletions unn04012/array/arr_rotate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function solution(numbers, direction) {
const answer = [];

if (direction === 'right') {
for (let i = 0; i < numbers.length - 1; i++) {
answer[i + 1] = numbers[i];
}
answer[0] = numbers[numbers.length - 1];
Comment on lines +5 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

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

현재 for을 사용하여 각 숫자를 이동시키는 것도 좋지만, 혹시 메서드를 사용하면 코드가 단순해져서 이런 방법도 추천드립니다.

  1. numbers.pop()으로 배열의 마지막 요소를 제거하고,
  2. numbers.unshift()로 제거한 요소를 배열의 맨 앞에 추가.
const last = numbers.pop(); 
numbers.unshift(last);

} else {
for (let i = numbers.length - 1; i >= 1; i--) {
answer[i - 1] = numbers[i];
}
answer[numbers.length - 1] = numbers[0];
}

return answer;
}

console.log(solution([4, 455, 6, 4, -1, 45, 6], 'left'));
9 changes: 9 additions & 0 deletions unn04012/array/cut_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.

현재 코드에서 for을 사용해도 작동은 하지만, 반복문은 모든 수를 하나씩 순회해야 하기 때문에, 이 문제에서도 불필요하다는 생각이 듭니다. 그런 면에서, slice메서드가 배열의 특정 기간을 쉽게 추출하는 기능을 제공해서 반복문 대신 return numbers.slice(num1, num2 + 1); 하시는것을 추천드립니다:)

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function solution(numbers, num1, num2) {
const answer = [];

for (let i = num1; i <= num2; i++) answer.push(numbers[i]);

return answer;
}

console.log(solution([1, 2, 3, 4, 5], 1, 3));
10 changes: 10 additions & 0 deletions unn04012/array/delete_min_num.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function solution(arr) {
const answer = [];
const minNum = Math.min(...arr);

for (const e of arr) {
if (e === minNum) continue;
answer.push(e);
}
Comment on lines +2 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

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

새로운 배열 answer를 생성하고, for문을 이용해서 조건을 체크하고 숫자들을 추가하고 있지만, 이 부분에서 새로운 배열을 생성하면서 조건도 체크해주는 filter 메서드도 한번 사용해보시는 것도 좋을것 같습니다:)

return answer.length ? answer : [-1];
}
Loading