ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JS 100์ œ] ๋ฌธ์ œ29 - ๋Œ€๋ฌธ์ž๋งŒ ์ง€๋‚˜๊ฐ€์„ธ์š”
    ๐Ÿ›  develop/CodingTest 2022. 12. 6. 17:00

    ๋ฌธ์ œ

    ์ง„๊ตฌ๋Š” ์˜์–ด ํ•™์› ์•„๋ฅด๋ฐ”์ดํŠธ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜ ์•„์ด๋“ค์€ ์•ŒํŒŒ๋ฒณ์„ ๊ณต๋ถ€ํ•˜๋Š” ํ•™์ƒ๋“ค์ธ๋ฐ ์˜ค๋Š˜์€ ๋Œ€๋ฌธ์ž ์“ฐ๊ธฐ ์‹œํ—˜์„ ๋ดค์Šต๋‹ˆ๋‹ค. ์•ŒํŒŒ๋ฒณ ํ•˜๋‚˜๋งŒ์„ ์ž…๋ ฅํ•˜๊ณ  ๊ทธ ์•ŒํŒŒ๋ฒณ์ด ๋Œ€๋ฌธ์ž์ด๋ฉด YES๋ฅผ ์•„๋‹ˆ๋ฉด NO๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”.

     

    ๋‚˜์˜ ํ’€์ด

    const txt = prompt("์˜์–ด ์•ŒํŒŒ๋ฒณ ํ•œ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
    const alpha = [
      "a",
      "b",
      "c",
      "d",
      "e",
      "f",
      "g",
      "h",
      "i",
      "j",
      "k",
      "l",
      "m",
      "n",
      "o",
      "p",
      "q",
      "r",
      "s",
      "t",
      "u",
      "v",
      "w",
      "x",
      "y",
      "z"
    ];
    
    if (alpha.findIndex((element) => element == txt) == -1) {
      console.log("YES(๋Œ€๋ฌธ์ž)");
    } else {
      console.log("NO(์†Œ๋ฌธ์ž)");
    }
    • ์ž…๋ ฅ๋ฐ›์„ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.
    • ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
    • findIndex ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์‹œ ๋งŒ์•ฝ alpha ๋ฐฐ์—ด ๋‚ด์— ์ž…๋ ฅ๋œ ๋ฌธ์ž๊ฐ€ ์—†๋‹ค๋ฉด -1 ์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, -1 ์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋Œ€๋ฌธ์ž๋ผ๊ณ  ์กฐ๊ฑด์‹์„ ์ž‘์„ฑํ•ด์ฃผ์—ˆ๋‹ค.
    • ์•„๋‹ˆ๋ผ๋ฉด ์ธ๋ฑ์Šค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— else๋กœ ๋‚˜๋จธ์ง€๋Š” ์ „๋ถ€ ์†Œ๋ฌธ์ž๋ผ๋Š” ์กฐ๊ฑด์‹์„ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

     

    ์•ŒํŒŒ๋ฒณ ํ•œ ๊ธ€์ž ๊ธฐ์ค€์ด ์•„๋‹ ๊ฒฝ์šฐ์—” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€.. ์ƒ๊ฐ์„ ํ•˜๋‹ค๊ฐ€ ๋‹ค๋ฅธ ๋ฐฉ์‹์„ ์ƒ๊ฐํ•ด๋ณด์•˜๋‹ค. 

    const txt = prompt("๋Œ€์†Œ๋ฌธ์ž ํŒ๋ณ„๊ธฐ์ž…๋‹ˆ๋‹ค");
    
    if (txt == txt.toUpperCase()) {
      console.log("YES(๋Œ€๋ฌธ์ž)");
    } else if (txt == txt.toLowerCase()) {
      console.log("NO(์†Œ๋ฌธ์ž)");
    } else {
      console.log("์†Œ๋ฌธ์ž ๋Œ€๋ฌธ์ž๋ฅผ ์„ž์–ด ์“ฐ์…จ๊ตฐ์š”");
    }
    • ๋ฌธ์ œ ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    • txt ๋กœ ๋ฌธ์žฅ์„ ๋ฐ›์•„์˜จ ํ›„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ toUpperCase() ์™€ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ toLowerCase() ๋ฅผ ์ ์šฉํ•œ txt ์™€ ๋น„๊ตํ•˜์—ฌ ๊ฐ’์„ ํŒ๋ณ„ํ•ด ๋‚ด๋Š” ๊ฒƒ์ด๋‹ค.

     

    ์ •๋‹ต

    const data = prompt('์•ŒํŒŒ๋ฒณ์„ ์ž…๋ ฅํ•˜์„ธ์š”.');
    
    if (data === data.toUpperCase()){
      console.log('YES');
    } else {
      console.log('NO');
    }
    • ์ •๋‹ต๋„ toUpperCase() ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€์ดํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    • ์‚ฌ์‹ค ๋Œ€๋ฌธ์ž ์†Œ๋ฌธ์ž๋ฅผ ์„ž์—ˆ์„๋•Œ! ์— ๋Œ€ํ•œ ์กฐ๊ฑด์‹์ด ํ•˜๋‚˜ ๋” ์žˆ๋Š” ๋‚˜์˜ ํ’€์ด๊ฐ€ ์ข‹์„ ๋“ฏ ํ•˜๋‹ค ^^ (์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค..)

     

    ๋Œ“๊ธ€