let's get IT with DAVINA ๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]Lv0. ์ง์ˆ˜์˜ ํ•ฉ ๋ณธ๋ฌธ

DEV_IN/๋ฌธ์ œํ’€์ด

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

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 3. 7. 01:10

โ“ ๋ฌธ์ œ ์„ค๋ช…

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

๐Ÿšซ ์ œํ•œ์‚ฌํ•ญ

0 < n ≤ 1000

๐Ÿ—๏ธ ์ž…์ถœ๋ ฅ ์˜ˆ

n result
10 30
4 6

๐Ÿ’ก ๋น„๋‹ˆ's ํ’€์ด

function solution(n) {
    let base=2;
    let sum=0;
    let arr=[]; //n์ดํ•˜์˜ ์ˆซ์ž๋“ค ์ค‘ ์ง์ˆ˜๋“ค๋งŒ ๋‹ด์„ ๋ฐฐ์—ด
    while(base<=n){
        arr.push(base);
        base=base+2;
    }
    //์ง์ˆ˜์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ
    for(let i=0;i<arr.length;i++){
        sum=sum+arr[i]
    }
    return sum;
}

๐Ÿ‘€ others' ํ’€์ด

function solution(n) {
    var answer = 0;

    for(let i=2 ; i<=n ; i+=2) //์ง์ˆ˜๋“ค๋งŒ ๋‚˜์—ดํ•ด์„œ
        answer += i; //๋ฐ”๋กœ ํ•ฉ ๊ตฌํ•˜๊ธฐ

    return answer;
}
๋ฌธ์ œ ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์•˜์ง€๋งŒ ์ข€ ๋” ํšจ์œจ์ ์ธ ์ฝ”๋“œ๋ฅผ ์ƒ๊ฐํ•ด ๋ณผ ํ•„์š”์„ฑ์„ ๋Š๊ผˆ๋‹น.. ๋‚œ ์ƒ๊ฐ์ด ๋งŽ์•„์„œ ๊ทธ๋Ÿฐ์ง€ ๋„ˆ๋ฌด ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•˜๋ ค๋Š” ๊ฒƒ ๊ฐ™๊ตฐ
Comments