๋ฐ์ํ
Problem Description
์ ์ n์ด ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, n ์ดํ์ ํ์๊ฐ ์ค๋ฆ์ฐจ์์ผ๋ก ๋ด๊ธด ๋ฐฐ์ด์ returnํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
Restrictions
- 1 ≤ n ≤ 100
Input/Output Example
- ์ ์ถ๋ ฅ ์ #1 10 ์ดํ์ ํ์๊ฐ ๋ด๊ธด ๋ฐฐ์ด [1, 3, 5, 7, 9]๋ฅผ returnํฉ๋๋ค.
- ์ ์ถ๋ ฅ ์ #2 15 ์ดํ์ ํ์๊ฐ ๋ด๊ธด ๋ฐฐ์ด [1, 3, 5, 7, 9, 11, 13, 15]๋ฅผ returnํฉ๋๋ค.
My solution
function solution(n) {
let array = [];
let i
for (i = 0; i <= n; i++) {
array.push(i)
}
return array.filter((x) => x%2 == 1)
}
Another solutions
function solution(n) {
var answer = [];
for (let i = 1; i<=n; i+=2) answer.push(i)
return answer;
}
const array = Array.from({ length: n + 1 }, (_, i) => i);
https://school.programmers.co.kr/learn/courses/30/lessons/120813
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/from
๋ฐ์ํ
'Problem Solution > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค/JS] n์ ๋ฐฐ์ ๊ณ ๋ฅด๊ธฐ (0) | 2023.09.12 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/JS] ๋๋ฌธ์์ ์๋ฌธ์ (0) | 2023.09.12 |
[ํ๋ก๊ทธ๋๋จธ์ค/JS] ์จ์ด์๋ ์ซ์์ ๋ง์ (1) (0) | 2023.09.11 |
[ํ๋ก๊ทธ๋๋จธ์ค/JS] ๋จธ์ฑ์ด๋ณด๋ค ํค ํฐ ์ฌ๋ (0) | 2023.09.07 |
[ํ๋ก๊ทธ๋๋จธ์ค/JS] ์ต๋๊ฐ ๋ง๋ค๊ธฐ (1) (0) | 2023.09.07 |