๐Ÿ”†[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ง์ˆ˜์˜ ํ•ฉ

๋ฆฌ๋น—ยท2023๋…„ 10์›” 19์ผ
0

JavaScript ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
6/7
post-custom-banner

๐Ÿ“Œ ๋‚ด ํ’€์ด

1. for ๋ฌธ

function solution(n) {
    let sum = 0;
    for(let i = 0; i<= n; i++){
        if(i % 2 === 0){
            sum += i;
        }
    }
    return sum;
}

๐Ÿ“Œ ๋‹ค๋ฅธ ํ’€์ด

1. for๋ฌธ

  • ๊ด„ํ˜ธ์•ˆ์„ ์ˆ˜์ •ํ•˜๋ฉด, ๋ฐ˜๋ณตํšŸ์ˆ˜๋ฅผ ์ค„์ผ์ˆ˜ ์žˆ๋‹ค.
function solution(n) {
    var answer = 0;

    for(let i=2 ; i<=n ; i+=2)
        answer += i;

    return answer;
}

2. ๋ฉ”์†Œ๋“œ ์ฒด์ด๋‹(์–ด๋ ต๋‹ค....)

function solution(n) {
  return Array(n)
    .fill()
    .map((_, i) => i + 1)
    .filter((v) => v % 2 === 0)
    .reduce((acc, cur) => acc + cur, 0);
}

์ฐธ๊ณ ์ž๋ฃŒ

profile
์–ด์ œ๋ณด๋‹ค ๋‚˜์€ ์˜ค๋Š˜์„ ์œ„ํ•ด
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€