JS) Random number

Ceciliaยท2023๋…„ 1์›” 2์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
31/36
post-thumbnail

๋‚ด๊ฐ€ ๋งŒ๋“  Random Number Game ์ฝ”๋“œ๋ฅผ ๊ธฐ๋กํ•ด๋ณด์ž!



<!--HTML-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>Random Number Game</h1>
    <form>
      <div>
        Generate a number between 0 and
        <input type="number" min="0" />
        <span style="visibility: hidden">์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”</span>
      </div>
      <div>
        Guess the number:
        <input type="number" min="0" />
        <button type="submit">Play!</button>
        <span style="visibility: hidden">์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”</span>
      </div>
    </form>
    <div class="infoDiv"></div>
    <div class="resultDiv"></div>
    <div class="warningDiv"></div>
    <script src="index.js"></script>
  </body>
</html>

//Javascript
const generateNumber = document.querySelector("form > :first-child > input");
const guessNumber = document.querySelector("form > :last-child > input");
const btn = document.querySelector("button");

const infoDiv = document.querySelector(".infoDiv");
const resultDiv = document.querySelector(".resultDiv");
const warningDiv = document.querySelector(".warningDiv");

const warningSpan1 = document.querySelector("form > :first-child > span");
const warningSpan2 = document.querySelector("form > :last-child > span");

//btn click -> ๋น„๊ต ์‹œ์ž‘
function clickBtn(event) {
  event.preventDefault();

  const getNumber = generateNumber.value; //์œ ์ €๊ฐ€ ์„ค์ •ํ•œ ์ˆซ์ž ๋ฒ”์œ„
  const getUserNumber = guessNumber.value; //ํ”„๋กœ๊ทธ๋žจ๊ณผ ๋น„๊ตํ•  ์ˆซ์ž

  //0 ~ generateNumber ์ดํ•˜ ๊ฐ’ ์ค‘ ์ปดํ“จํ„ฐ๊ฐ€ ๋žœ๋ค์œผ๋กœ ์ง€์ •ํ•œ ๊ฐ’.
  const machineNumber = Number(Math.round(Math.random() * getNumber));

  //input validation
  if (!getNumber) {
    warningSpan1.style.visibility = "visible";
    warningSpan1.style.color = "red";
    warningSpan2.style.visibility = "hidden";
  } else if (!getUserNumber) {
    warningSpan2.style.visibility = "visible";
    warningSpan2.style.color = "red";
    warningSpan1.style.visibility = "hidden";
  } else {
    warningSpan1.style.visibility = "hidden";
    warningSpan2.style.visibility = "hidden";

    //information
    infoDiv.innerHTML = `You chose: <span style="font-weight: bold">${guessNumber.value}</span>, the machine chose: <span style="font-weight: bold">${machineNumber}</span>`;

    //์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์ด ๋น„๊ตํ•  ๊ฐ’๋ณด๋‹ค ํด๋•Œ
    if (getNumber < getUserNumber) {
      warningDiv.innerHTML = "์ž…๋ ฅํ•œ ๊ฐ’์ด ์„ค์ •ํ•œ ๋ฒ”์œ„๊ฐ’๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค!";
      warningDiv.style.color = "pink";
      infoDiv.innerHTML = "";
      resultDiv.innerHTML = "";

      //win
    } else if (getUserNumber > machineNumber) {
      resultDiv.innerHTML = "You won!";
      warningDiv.innerHTML = "";

      //lose
    } else if (getUserNumber < machineNumber) {
      resultDiv.innerHTML = "You lost!";
      warningDiv.innerHTML = "";

      //tie
    } else if (getUserNumber == machineNumber) {
      resultDiv.innerHTML = "ended in a tie!";
      warningDiv.innerHTML = "";
    }
  } //if๋ฌธ
} //clickBtn

btn.addEventListener("click", clickBtn);

//**ํ•ด๊ฒฐํ•ด์•ผํ•  ๋ฌธ์ œ**
//1. machineNumber 0 ์•ˆ๋œธ => Math.round()๋กœ 0๋„ ๋‚˜์˜ค๊ฒŒ ํ•ด๊ฒฐํ•จ. ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— parseInt() ์‚ฌ์šฉ X
//2. ์œ ํšจ์„ฑ๊ฒ€์‚ฌ(Generate number, Guess number) => ๋ฒ„ํŠผ ํด๋ฆญ์‹œ visibility: hidden -> visible๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ํ•ด๊ฒฐํ•จ.
//3. ์œ ํšจ์„ฑ๊ฒ€์‚ฌ ํ†ต๊ณผ์‹œ warningDiv ๋‹ค์‹œ hidden์œผ๋กœ ๋ณ€๊ฒฝ => ok.
//4. ๋ฒ„ํŠผ ๋‹ค์‹œ ํด๋ฆญ์‹œ ๊ธฐ์กด์— ์žˆ๋˜ info, result ์—†์• ๊ธฐ => innerHTML ๋‹ค์‹œ ์ดํ•ดํ•˜์—ฌ ํ•ด๊ฒฐํ•จ.
//5. Guess number๊ฐ€ Generate number๋ณด๋‹ค ํฌ๋ฉด ์ž‘๋™ ์•ˆ ๋˜๊ฒŒ => warningDiv ์ƒ์„ฑํ•˜์—ฌ ํ•ด๊ฒฐํ•จ.
//6. ์Œ์ˆ˜๋Š” ์•ˆ ๋จ! =>  min="0" ํ•ด๊ฒฐ.

๋‚˜๋Š” addEventListener ๊ธฐ๋Šฅ ์ค‘ submit์ด ์•„๋‹Œ click์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.
submit์œผ๋กœ ํ•˜๋ ค๋‹ˆ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ๋ถˆํŽธํ•ด์ ธ์„œ click์œผ๋กœ ํ–ˆ๋‹ค.
HTML input์— required๋ฅผ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ,
required๋กœ ๋‚˜์˜ค๋Š” ์•Œ๋žŒ์ด ๊ทธ๋‹ฅ ์˜ˆ์˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค... ๐Ÿ˜…


๊ทธ๋ฆฌ๊ณ  Math.round() ์‚ฌ์šฉ์€ ์กฐ๊ธˆ ๋” ์•Œ์•„๋ณด๊ณ  ์จ์•ผํ• ๋“ฏํ•˜๋‹ค.
์•„๋งˆ ๋‚ด์ผ ์กฐ๊ธˆ ๋” ์ •๋ˆ๋œ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์˜ฌ๋ฆฌ์ง€ ์•Š์„๊นŒ!




๐ŸŽ‰ 2023.01.03 => ์•„๋ž˜ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ!

// const warningSpan1 = document.querySelector("form > :first-child > span");
// const warningSpan2 = document.querySelector("form > :last-child > span");
const [warningSpan1, warningSpan2] = document.getElementsByTagName("span");
profile
'์ด๊ฒŒ ์ตœ์„ ์ผ๊นŒ?'๋ผ๋Š” ๊ณ ์ฐฐ์„ ํ†ตํ•ด ๋์—†์ด ์„ฑ์žฅํ•˜๊ณ , ๊ทธ ๊ณผ์ •์„ ์ฆ๊ธฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์„ ์ƒ๊ฐํ•˜๋ฉฐ ์ตœ์„ ์˜ ํŽธ์˜์„ฑ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€