728x90
๐ 04 - 1๋ถํฐ N๊น์ง ํฉ ์ถ๋ ฅํ๊ธฐ
๋จ์ํ๊ฒ ๊ฐ์ ๋์ ํ๋ ๋ฌธ์ ๋ค. sum
๊ณต์์ ์ฌ์ฉํ๊ฑฐ๋ ๋ฐ๋ณต๋ฌธ
์ ์ฌ์ฉํ๋ค.
console.log(solution(6));
// sum๊ณต์
function solution(n) {
return (n * (n + 1)) / 2;
}
// ๋ฐ๋ณต๋ฌธ
function solution(n) {
let answer = 0;
for (let i = 1; i <= n; i++) {
answer += i;
}
return answer;
}
๋ฐ์ํ
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 06 - ํ์ (0) | 2021.08.11 |
---|---|
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 05 - ์ต์๊ฐ ๊ตฌํ๊ธฐ (0) | 2021.08.10 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 03 - ์ฐํ ๊ฐ์ (0) | 2021.08.10 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 02 - ์ผ๊ฐํ ํ๋ณํ๊ธฐ (0) | 2021.08.09 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 01 - ์ธ ์ ์ค ์ต์๊ฐ (2) | 2021.08.09 |
๋๊ธ