๐ŸŒˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ๋“ฑ์ฐจ์ˆ˜์—ด์˜ ํŠน์ •ํ•œ ํ•ญ๋งŒ ๋”ํ•˜๊ธฐ

Eun_zii 2024. 4. 16. 16:09

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.0

- ๋“ฑ์ฐจ์ˆ˜์—ด์˜ ํŠน์ •ํ•œ ํ•ญ๋งŒ ๋”ํ•˜๊ธฐ

 

๐Ÿ’š ๋ฌธ์ œ์„ค๋ช…

 

๐Ÿ’š ์ œํ•œ์กฐ๊ฑด

  • 1 ≤ a ≤ 100
  • 1 ≤ d ≤ 100
  • 1 ≤ included์˜ ๊ธธ์ด ≤ 100
  • included์—๋Š” true๊ฐ€ ์ ์–ด๋„ ํ•˜๋‚˜ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’š ํ’€์ด

function solution(a, d, included) {
    let answer = 0;
    for (let i = 0; i < included.length; i++) {
        if (included[i] === true ) {
            answer += a + i * d;
        }
    }
    return answer;
}

 

โœ… 

for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ included ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ์ฃผ๊ณ , 

included ๋ฐฐ์—ด์€ boolean ๊ฐ’์ด ๋“ค์–ด์žˆ์–ด์„œ ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ํ•ญ๋ชฉ์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

๊ทธ๋ฆฌ๊ณ  i๊ฐ€ true์ธ์ง€ ํ™•์ธํ•œ๋‹ค. 

๋งŒ์•ฝ included[i]๊ฐ€ true๋ผ๋ฉด, ํ•ด๋‹น ํ•ญ๋ชฉ์ด ํฌํ•จ๋˜์–ด ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋ฏ€๋กœ, ๋“ฑ์ฐจ์ˆ˜์—ด์—์„œ ํ•ด๋‹น ์œ„์น˜์˜ ํ•ญ๋ชฉ์„ ๋”ํ•ด์ค€๋‹ค.

์ด๋•Œ, ๋“ฑ์ฐจ์ˆ˜์—ด์˜ i+1๋ฒˆ์งธ ํ•ญ๋ชฉ์˜ ๊ฐ’์€ a + i * d ์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

์ด ๊ฐ’์„ answer์— ๋”ํ•ด์ค€ํ›„ returnํ•œ๋‹ค.

728x90