From 43d4741292d9f136d4502204c8f77055e29aaff4 Mon Sep 17 00:00:00 2001 From: beurmuz Date: Sun, 5 Jun 2022 08:06:02 +0900 Subject: [PATCH] =?UTF-8?q?#12=2022.06.05=20>=20x=EB=A7=8C=ED=81=BC=20?= =?UTF-8?q?=EA=B0=84=EA=B2=A9=EC=9D=B4=20=EC=9E=88=EB=8A=94=20n=EA=B0=9C?= =?UTF-8?q?=EC=9D=98=20=EC=88=AB=EC=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/programmers/Lv_1/README.md | 3 ++- src/programmers/Lv_1/xLength.js | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/programmers/Lv_1/xLength.js diff --git a/src/programmers/Lv_1/README.md b/src/programmers/Lv_1/README.md index e0656af9..6f5dba5f 100644 --- a/src/programmers/Lv_1/README.md +++ b/src/programmers/Lv_1/README.md @@ -8,4 +8,5 @@ |[자릿수 더하기](./positionSum.js)|22.06.03|O|X| |[평균구하기](./average.js)|22.06.04|O|X| |[자연수 뒤집어 배열로 만들기](./reverseNumber.js)|22.06.04|O|X| -|[짝수와 홀수](./oddOrEven.js)|22.06.04|O|X| \ No newline at end of file +|[짝수와 홀수](./oddOrEven.js)|22.06.04|O|X| +|[x만큼 간격이 있는 n개의 숫자](./xLength.js)}22.06.05|O|X| \ No newline at end of file diff --git a/src/programmers/Lv_1/xLength.js b/src/programmers/Lv_1/xLength.js new file mode 100644 index 00000000..5dc253ff --- /dev/null +++ b/src/programmers/Lv_1/xLength.js @@ -0,0 +1,27 @@ +/* + 내 풀이 + - 처음에 i = i*x를 해주니 i값이 계속 더한값으로 갱신되는 바람에 다른 값이 출력됨 + - 연산한 값을 sum이라는 변수에 저장하고, sum을 push함 +*/ +function solution(x, n) { + var answer = []; + for(let i = 1; i <= n; i++) { + sum = i*x; + answer.push(sum); + } + return answer; +} + + +/* + 다른 풀이 + - Array(n).fill(x)을 이용해 x로 다 채워놓은 후 + - map으로 안의 값 바꿔주는 방법 +*/ + +2 +3 +4 +function solution(x, n) { + return Array(n).fill(x).map((v, i) => (i + 1) * v) +} \ No newline at end of file