Problem Solution/Programmers

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

yuri lee 2023. 8. 25. 20:13
๋ฐ˜์‘ํ˜•

 

Problem Description

์ •์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ, n์ดํ•˜์˜ ์ง์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

Restrictions

  • 0 < n ≤ 1000

 

Input/Output Example

  • ์ž…์ถœ๋ ฅ ์˜ˆ #1 n์ด 10์ด๋ฏ€๋กœ 2 + 4 + 6 + 8 + 10 = 30์„ return ํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #2 n์ด 4์ด๋ฏ€๋กœ 2 + 4 = 6์„ return ํ•ฉ๋‹ˆ๋‹ค.

 

My solution

function solution(n) {
    let sum = 0;

    for (let i = 2; i <= n; i += 2) {
        sum += i;
    }
    return sum;
}

 


Reference: https://school.programmers.co.kr/learn/courses/30/lessons/120831

๋ฐ˜์‘ํ˜•