Input + Output = True
ํ๋ก๊ทธ๋๋จธ์ค - ๋ ์ ์ ์ฌ์ด์ ํฉ ๋ณธ๋ฌธ
๋ ์ ์ ์ฌ์ด์ ํฉ
๋ฌธ์ ์ค๋ช
๋ ์ ์ a, b๊ฐ ์ฃผ์ด์ก์ ๋
a์ b ์ฌ์ด์ ์ํ ๋ชจ๋ ์ ์์ ํฉ์ ๋ฆฌํดํ๋ ํจ์, solution์ ์์ฑํ์ธ์.
์๋ฅผ ๋ค์ด a = 3, b = 5์ธ ๊ฒฝ์ฐ, 3 + 4 + 5 = 12์ด๋ฏ๋ก 12๋ฅผ ๋ฆฌํดํฉ๋๋ค.
์ ํ ์กฐ๊ฑด
a์ b๊ฐ ๊ฐ์ ๊ฒฝ์ฐ๋ ๋ ์ค ์๋ฌด ์๋ ๋ฆฌํดํ์ธ์.
function solution(a, b, answer = 0) {
let numMin = Math.min(a, b)
let numMax = Math.max(a, b)
for(let i=numMin; i<=numMax; i++){
answer += i
}
return answer
}
console.log(solution(3, 5))
์ต์๊ฐ, ์ต๋๊ฐ์ ๊ตฌํ ํ for๋ฌธ์ ํตํ ๋ฐ๋ณต ํฉ์ฐ์ผ๋ก ์ถ๋ ฅ.
๋ ์๊ฐ ๊ฐ์ ๊ฒฝ์ฐ, ํด๋น ์ซ์ ์ถ๋ ฅ.
https://school.programmers.co.kr/learn/courses/30/lessons/12912?language=javascript
'๐ตAlgorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๊ณ ๋ฆฌ์ฆ - ํธ๋ฆฌ ๋ง๋ค๊ธฐ (0) | 2022.12.25 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค - ์๋ ์ซ์ ๋ํ๊ธฐ (0) | 2022.12.24 |
ํ๋ก๊ทธ๋๋จธ์ค - ๊ฐ์ด๋ฐ ๊ธ์ ๊ฐ์ ธ์ค๊ธฐ (0) | 2022.12.23 |
ํ๋ก๊ทธ๋๋จธ์ค - ์ง์์ ํ์ (0) | 2022.12.23 |
ํ๋ก๊ทธ๋๋จธ์ค - ์ง์ฌ๊ฐํ ๋ณ์ฐ๊ธฐ (0) | 2022.12.22 |
Comments