๋ฐ์ํ
Problem Description
์ค์๊ฐ์ ์ด๋ค ์ฃผ์ด์ง ๊ฐ๋ค์ ํฌ๊ธฐ์ ์์๋๋ก ์ ๋ ฌํ์ ๋ ๊ฐ์ฅ ์ค์์ ์์นํ๋ ๊ฐ์ ์๋ฏธํฉ๋๋ค. ์๋ฅผ ๋ค์ด 1, 2, 7, 10, 11์ ์ค์๊ฐ์ 7์ ๋๋ค. ์ ์ ๋ฐฐ์ด array๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์ค์๊ฐ์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด๋ณด์ธ์.
Restrictions.
- array์ ๊ธธ์ด๋ ํ์์ ๋๋ค.
- 0 < array์ ๊ธธ์ด < 100
- -1,000 < array์ ์์ < 1,000
Input/Output Example
- ์ ์ถ๋ ฅ ์ #1 ๋ณธ๋ฌธ๊ณผ ๋์ผํฉ๋๋ค.
- ์ ์ถ๋ ฅ ์ #2 9, -1, 0์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๋ฉด -1, 0, 9์ด๊ณ ๊ฐ์ฅ ์ค์์ ์์นํ๋ ๊ฐ์ 0์ ๋๋ค.
My solution
function solution(array) {
const result = array.sort((a, b) => a - b)
return result[Math.floor(result.length / 2)]
}
Math.ceil() : ์์์ ์ฌ๋ฆผ, ์ ์ ๋ฐํ Math.floor() : ์์์ ๋ฒ๋ฆผ, ์ ์ ๋ฐํ Math.round() : ์์์ ๋ฐ์ฌ๋ฆผ, ์ ์ ๋ฐํ
https://school.programmers.co.kr/learn/courses/30/lessons/120811
๋ฐ์ํ
'Problem Solution > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค/JS] ํผ์ ๋๋ ๋จน๊ธฐ (3) (1) | 2023.10.17 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/JS] ์ ์ ์์น ๊ตฌํ๊ธฐ (0) | 2023.10.17 |
[ํ๋ก๊ทธ๋๋จธ์ค/JS] ์ธ๊ณํ์ฑ์ ๋์ด (0) | 2023.10.17 |
[ํ๋ก๊ทธ๋๋จธ์ค/JS] ๋ฌธ์์ด ์ ๋ ฌํ๊ธฐ (1) (0) | 2023.09.26 |
[ํ๋ก๊ทธ๋๋จธ์ค/JS] ๊ฐ์ฅ ํฐ ์ ์ฐพ๊ธฐ (0) | 2023.09.25 |