๋ฌธ์ œ

๋ฌธ์ž์—ด s๋Š” ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ณต๋ฐฑ๋ฌธ์ž๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ๋Œ€๋ฌธ์ž๋กœ, ํ™€์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • ๋ฌธ์ž์—ด ์ „์ฒด์˜ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹ˆ๋ผ, ๋‹จ์–ด(๊ณต๋ฐฑ์„ ๊ธฐ์ค€)๋ณ„๋กœ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ํŒ๋‹จํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋กœ ๋ณด์•„ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

ํ’€์ด

๋ฌธ์ž์—ด์˜ ์ง์ˆ˜ ์ธ๋ฑ์Šค๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ , ํ™€์ˆ˜ ์ธ๋ฑ์Šค๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค. 

 

๊ผผ๊ผผํ•˜๊ฒŒ ์ง์ˆ˜ ํ™€์ˆ˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ–ˆ๋‹ค. ์˜ˆ์‹œ์— ์žˆ๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ†ต๊ณผ์ธ๋ฐ

์ œ์ถœํ•ด๋ณด๋ฉด ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ 16๊ฐœ์ค‘์— 4๊ฐœ๋ฐ–์— ํ†ต๊ณผ๊ฐ€ ๋˜์ง€์•Š์•˜๋‹ค.

๋ฌธ์ œ๋ฅผ ๊ผผ๊ผผํ•˜๊ฒŒ ์ฝ์–ด์•ผํ–ˆ๋‹ค. 

try 

hello

world

๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ์„œ ๋‹ค์‹œ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„์ค˜์•ผํ–ˆ๋‹ค. 

 

 

์ฒ˜์Œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

const solution = (s) => {
  let answer = '';
  let string = s.split('');

  for (let i = 0; i < string.length; i++) {

      if (i % 2 === 0) {
        answer += string[i].toUpperCase();
      } else {
        answer += string[i].toLowerCase();
      }
  }
  return answer;
}

const s = "try hello world";
console.log(solution(s))

 

์ตœ์ข… ์ฝ”๋“œ

const solution = (s) => {
  let answer = '';
  let index = 0;

  for (let i = 0; i < s.length; i++) {
    if (s[i] === ' ') {
      index = 0; // ๊ณต๋ฐฑ์ด๋ฉด ์ธ๋ฑ์Šค ๋‹ค์‹œ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
      answer += ' ';
    } else {
      if (index % 2 === 0) {
        answer += s[i].toUpperCase();
      } else {
        answer += s[i].toLowerCase();
      }
      index++; // ๊ณต๋ฐฑ์ด ์•„๋‹๋•Œ ์ธ๋ฑ์Šค ์ฆ๊ฐ€
    }
  }
  return answer;
}

const s = "try hello world";
console.log(solution(s));

 

ํ†ต๊ณผ๊ฐ€ ์•ˆ๋˜๋ฉด ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ณด์ž. 

๊ทธ ์ „์— ๋ฌธ์ œ๋ฅผ ๊ผผ๊ผผํ•˜๊ฒŒ ์ฝ์ž.

+ Recent posts