๋ด๊ฐ ๋ง๋ 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");