diff --git a/Moonjonghoo/array/create_stars_at_intersections.js b/Moonjonghoo/array/create_stars_at_intersections.js new file mode 100644 index 0000000..81bd3d1 --- /dev/null +++ b/Moonjonghoo/array/create_stars_at_intersections.js @@ -0,0 +1,4 @@ +function solution(line) { + var answer = []; + return answer; +} diff --git a/Moonjonghoo/array/divisible_number_array.js b/Moonjonghoo/array/divisible_number_array.js new file mode 100644 index 0000000..767fcbe --- /dev/null +++ b/Moonjonghoo/array/divisible_number_array.js @@ -0,0 +1,17 @@ +function solution(arr, divisor) { + var answer = []; + for (let i = 0; i < arr.length; i++) { + if (arr[i] % divisor === 0) { + answer.push(arr[i]); + } + } + if (answer.length === 0) { + answer = [-1]; + } + + answer.sort(function (a, b) { + return a - b; + }); + + return answer; +} diff --git a/Moonjonghoo/array/duplicate_number_count.js b/Moonjonghoo/array/duplicate_number_count.js new file mode 100644 index 0000000..615f571 --- /dev/null +++ b/Moonjonghoo/array/duplicate_number_count.js @@ -0,0 +1,9 @@ +function solution(array, n) { + var answer = 0; + for (let i = 0; i < array.length; i++) { + if (array[i] === n) { + answer++; + } + } + return answer; +} diff --git a/Moonjonghoo/array/length_of_an_array_element.js b/Moonjonghoo/array/length_of_an_array_element.js new file mode 100644 index 0000000..4750486 --- /dev/null +++ b/Moonjonghoo/array/length_of_an_array_element.js @@ -0,0 +1,9 @@ +function solution(strlist) { + var answer = []; + for (let i = 0; i < strlist.length; i++) { + answer.push(strlist[i].length); + } + return answer; +} + +console.log(solution(["We", "are", "the", "world!"])); diff --git a/Moonjonghoo/array/matrix_addition.js b/Moonjonghoo/array/matrix_addition.js new file mode 100644 index 0000000..f99ea27 --- /dev/null +++ b/Moonjonghoo/array/matrix_addition.js @@ -0,0 +1,23 @@ +function solution(arr1, arr2) { + var answer = []; + for (let i = 0; i < arr1.length; i++) { + let minarr = []; + for (let j = 0; j < arr1[i].length; j++) { + minarr.push(arr1[i][j] + arr2[i][j]); + } + answer.push(minarr); + } + return answer; +} +console.log( + solution( + [ + [1, 2], + [2, 3], + ], + [ + [3, 4], + [5, 6], + ] + ) +); diff --git a/Moonjonghoo/array/matrix_multiplication.js b/Moonjonghoo/array/matrix_multiplication.js new file mode 100644 index 0000000..85c5ed8 --- /dev/null +++ b/Moonjonghoo/array/matrix_multiplication.js @@ -0,0 +1,17 @@ +function solution(arr1, arr2) { + const newArr = []; + + for (let i = 0; i < arr1.length; i++) { + let result = []; + for (let j = 0; j < arr2[0].length; j++) { + let elem = 0; + for (let k = 0; k < arr2.length; k++) { + // arr1[0].length도 가능. + elem += arr1[i][k] * arr2[k][j]; + } + result.push(elem); + } + newArr.push(result); + } + return newArr; +} diff --git a/Moonjonghoo/array/remove_smallest_number.js b/Moonjonghoo/array/remove_smallest_number.js new file mode 100644 index 0000000..5a78728 --- /dev/null +++ b/Moonjonghoo/array/remove_smallest_number.js @@ -0,0 +1,16 @@ +function solution(arr) { + var answer = []; + if (arr.length === 1) { + answer = [-1]; + } else { + let min = Math.min(...arr); + for (let i = 0; i < arr.length; i++) { + if (arr[i] !== min) { + answer.push(arr[i]); + } + } + } + return answer; +} + +solution([4, 3, 2, 1]); diff --git a/Moonjonghoo/array/rotate_an_array.js b/Moonjonghoo/array/rotate_an_array.js new file mode 100644 index 0000000..b11c393 --- /dev/null +++ b/Moonjonghoo/array/rotate_an_array.js @@ -0,0 +1,20 @@ +function solution(numbers, direction) { + var answer = []; + //배열내부에 위치를 변경시키는 방법 + for (let i = 0; i < numbers.length; i++) { + if (direction === "right") { + let start = numbers[numbers.length - 1]; + answer = numbers.slice(0, numbers.length - 1); + answer.unshift(start); + } + if (direction === "left") { + let start = numbers[0]; + answer = numbers.slice(1, numbers.length); + answer.push(start); + } + } + + return answer; +} + +console.log(solution([1, 2, 3], "right")); diff --git a/Moonjonghoo/array/slice_an_array.js b/Moonjonghoo/array/slice_an_array.js new file mode 100644 index 0000000..8b1e639 --- /dev/null +++ b/Moonjonghoo/array/slice_an_array.js @@ -0,0 +1,7 @@ +function solution(numbers, num1, num2) { + var answer = []; + answer = numbers.slice(num1, num2 + 1); + return answer; +} + +console.log(solution([1, 2, 3, 4, 5], 1, 3));