๐Ÿค ์€์ง€log ๐Ÿค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ˆ˜ ์กฐ์ž‘ํ•˜๊ธฐ 1 ๋ณธ๋ฌธ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ˆ˜ ์กฐ์ž‘ํ•˜๊ธฐ 1

Eun_zii 2023. 12. 28. 09:36

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

- ์ˆ˜ ์กฐ์ž‘ํ•˜๊ธฐ 1

 

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

  • ์ •์ˆ˜ n๊ณผ ๋ฌธ์ž์—ด control์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. control์€ "w", "a", "s", "d"์˜ 4๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, control์˜ ์•ž์—์„œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ฌธ์ž์— ๋”ฐ๋ผ n์˜ ๊ฐ’์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
  • "w" : n์ด 1 ์ปค์ง‘๋‹ˆ๋‹ค.
  • "s" : n์ด 1 ์ž‘์•„์ง‘๋‹ˆ๋‹ค.
  • "d" : n์ด 10 ์ปค์ง‘๋‹ˆ๋‹ค.
  • "a" : n์ด 10 ์ž‘์•„์ง‘๋‹ˆ๋‹ค.
  • ์œ„ ๊ทœ์น™์— ๋”ฐ๋ผ n์„ ๋ฐ”๊ฟจ์„ ๋•Œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋‚˜์˜ค๋Š” n์˜ ๊ฐ’์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

 

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

  • -100,000 ≤ n ≤ 100,000
  • 1 ≤ control์˜ ๊ธธ์ด ≤ 100,000
  • control์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž "w", "a", "s", "d"๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

 

๐Ÿ’š ํ’€์ด

function solution(n, control) {
    for (let i = 0; i < control.length; i++) {
        if(control[i] === "d") {
            n += 10
        } else if (control[i] === "a") {
            n -= 10 
        } else if (control[i] === "w") {
            n += 1
        } else if (control[i] === "s") {
            n -= 1
        }
    }
    return n;
}

 

โœ… for๋ฌธ์œผ๋กœ control ๋ฐ˜๋ณตํ›„ if๋ฌธ์œผ๋กœ ์กฐ๊ฑด ๋‹ฌ๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ณ 

'n'์— ์กฐ๊ฑด๋ณ„๋กœ += , -= ํ•ด์ค€ํ›„ ๋งˆ์ง€๋ง‰์— 'n' ์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ์ตœ์ข…๊ฐ’์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

 
728x90