ํด์ฆ ๋ง๋ค๊ธฐ 03
์ด๋ฒ ์๊ฐ์๋ ๊ทธ ๋์ ๋ฐฐ์ ๋ JavaScript๋ฅผ ์ด์ฉํ์ฌ ํด์ฆ๋ฅผ ๋ง๋ค์ด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ด๋ฒ ํ์ต ๋ด์ฉ์ ์ ํ์ฉํ์ฌ ์น๋์์ธ๊ธฐ๋ฅ์ฌ ์ํ ๊ณต๋ถ ์ด์ฌํ ํด ๋ด
์๋ค!
๋ณ์(let)์ ์์(const)์ ์ฟผ๋ฆฌ ์
๋ ํฐ ๊ฐ์ ์ ์ฅ์ํค๊ณ , ๊ทธ ๋์ ๋ฐฐ์ด ์คํฌ๋ฆฝํธ๋ฅผ
ํตํด ๋ฌธ์ ์ ๋ณด๋ฅผ HTML ํ๊ทธ์ ์ถ๋ ฅ์ํค๊ณ , ์ ๋ต์ ์จ๊ฒผ๋ค๊ฐ ๋ฒํผ์ ๋๋ฌ ๋ณด์ด๊ฒ ํ๋
๋ฑ์ ๋ค์ํ ํจ๊ณผ๋ค์ ๊ตฌํ์์ผ๋ด
์๋ค!
์ฃผ๊ด์(์ฌ๋ฌ ๋ฌธ์ ) ์ ํ
์ธ ๋ฒ์งธ ์ฃผ๊ด์(์ฌ๋ฌ ๋ฌธ์ ) ์ ํ์ ์๋ฐ์คํฌ๋ฆฝํธ ์์ค์ ๋ถ์ฐ ์ค๋ช
(์ฃผ์ ์ฒ๋ฆฌ๋จ)์ด๋ค.
HTML๊ณผ CSS ์์ค๋ฅผ ํจ๊ป ๋ณด๊ณ ์ถ๋ค๋ฉด ๋งจ ์๋์ '์๋ณธ ์์ค ๋ณด๊ธฐ' ๋ฒํผ์ ๋๋ฅผ ๊ฒ.
// ์ ํ์ : ์์ const๋ฅผ ํ์ํ ๋งํผ ์ ์ธํ ๋ค์ HTML ์์ญ์ div, ์๋งจํฑ ํ๊ทธ ๋ฑ์ Class๋ช
๊ณผ ์ฐ๊ฒฐ์ํจ๋ค.
// ๋ฌธ์ (<div class="quiz">)๊ฐ ์ฌ๋ฌ๊ฐ์ด๋ฏ๋ก ๋ค์ All์ ๊ผญ! ๋ถ์ฌ์ฃผ๊ธฐ
const quizType = document.querySelectorAll(".quiz__type"); // ํด์ฆ ์ข
๋ฅ
const quizNumber = document.querySelectorAll(".quiz__question .number"); // ํด์ฆ ๋ฒํธ
const quizAsk = document.querySelectorAll(".quiz__question .ask"); // ํด์ฆ ์ง๋ฌธ
const quizConfirm = document.querySelectorAll(".quiz__answer .confirm"); // ์ ๋ต ํ์ธ ๋ฒํผ
const quizResult = document.querySelectorAll(".quiz__answer .result"); // ์ ๋ต ๊ฒฐ๊ณผ
const quizInput = document.querySelectorAll(".quiz__answer .input"); // ์ฌ์ฉ์ ์ ๋ต
const quizView = document.querySelectorAll(".quiz__view"); // ๊ฐ์์ง
// ๋ฌธ์ ์ ๋ณด
const quizInfo = [
{
answerType: "์น๋์์ธ๊ธฐ๋ฅ์ฌ 2016๋
4ํ",
answerNum: "1",
answerAsk: "๋จผ์ ๋ณธ ์์ ์ํฅ์ผ๋ก ๋์ค์ ๋ณด๋ ์์ด ๋ค๋ฅด๊ฒ ๋ณด์ด๋ ํ์์?",
answerResult: "๊ณ์๋๋น"
},
{
answerType: "์น๋์์ธ๊ธฐ๋ฅ์ฌ 2016๋
4ํ",
answerNum: "2",
answerAsk: "HTML ๋ฌธ์์ ์์์ ์๋ ค์ฃผ๋ ํ๊ทธ๋?",
answerResult: "<html>"
},
{
answerType: "์น๋์์ธ๊ธฐ๋ฅ์ฌ 2016๋
4ํ",
answerNum: "3",
answerAsk: "E-mail ์ก์ ์์ ์ฌ์ฉ๋๋ ํ๋กํ ์ฝ์?",
answerResult: "SMTP"
},
{
answerType: "์น๋์์ธ๊ธฐ๋ฅ์ฌ 2016๋
4ํ",
answerNum: "4",
answerAsk: "์น์ฌ์ดํธ ์ ์ ๋จ๊ณ ์ค ์ฌ์ดํธ์ ๋ชฉ์ ๊ณผ ์ฌ์ฉ์ ๋ถ์์ ๋ฐ๋ผ ์ฌ์ดํธ์ ๋์์ธ ๋ฐฉํฅ์ ์ค์ ํ๋ ๋จ๊ณ๋?",
answerResult: "์ฝ์
ํธ ๋์ถ"
}
]
์์ ์์์ ๊ฐ์ด ์์(const)๋ฅผ ํ์ํ ๋งํผ ์์ฑํ ํ HTML ์์ญ์ div, ์๋งจํฑ ํ๊ทธ ๋ฑ์ class๋ช
๊ณผ ์ฐ๊ฒฐ์ํจ๋ค.
๋ํ ์ด๋ฒ ํฌ์คํ
์์๋ ๋ฌธ์ ๋ฅผ ์ฌ๋ฌ๊ฐ ๋ง๋ค์๊ธฐ ๋๋ฌธ์ document.querySelector ๋ค์ All์ ๋ฐ๋์ ๋ถ์ฌ์ผ ํ๋ค.
๋ฌธ์ ์ ๋ณด๋ ๋ฐฐ์ด ์์ ๊ฐ์ฒด๊ฐ ๋ด๊ธด ํํ๋ก ์
๋ ฅ์์ผฐ๋ค.
// ๋ฌธ์ ์ถ๋ ฅ : ์์์ ๋ฐฉ๋ฒ
// textContent๊ฐ innerText๋ณด๋ค ์ฌ์ฉ๋น๋ ๋ ๋์
// quizType[0].textContent = quizInfo[0].answerType;
// quizType[1].textContent = quizInfo[1].answerType;
// quizType[2].textContent = quizInfo[2].answerType;
// quizType[3].textContent = quizInfo[3].answerType;
// quizNumber[0].textContent = "0" + quizInfo[0].answerNum + ". ";
// quizNumber[1].textContent = "0" + quizInfo[1].answerNum + ". ";
// quizNumber[2].textContent = "0" + quizInfo[2].answerNum + ". ";
// quizNumber[3].textContent = "0" + quizInfo[3].answerNum + ". ";
// quizAsk[0].textContent = quizInfo[0].answerAsk;
// quizAsk[1].textContent = quizInfo[1].answerAsk;
// quizAsk[2].textContent = quizInfo[2].answerAsk;
// quizAsk[3].textContent = quizInfo[3].answerAsk;
// quizResult[0].textContent = quizInfo[0].answerResult;
// quizResult[1].textContent = quizInfo[1].answerResult;
// quizResult[2].textContent = quizInfo[2].answerResult;
// quizResult[3].textContent = quizInfo[3].answerResult;
// ๋ฌธ์ ์ถ๋ ฅ : for๋ฌธ
// for(let i = 0; i < quizInfo.length; i++) { // 4 ๋์ quizInfo.length๋ฅผ ์ฐ๋ฉด ๋ฌธ์ ๊ฐ ์ถ๊ฐ๋ผ๋ ์์ ํ ํ์ X
// quizType[i].textContent = quizInfo[i].answerType;
// quizNumber[i].textContent = "0" + quizInfo[i].answerNum + ". ";
// quizAsk[i].textContent = quizInfo[i].answerAsk;
// quizResult[i].textContent = quizInfo[i].answerResult;
// }
// ๋ฌธ์ ์ถ๋ ฅ : forEach๋ฌธ
// function ๊ดํธ ์์ e๋ฅผ ๊ผญ ์จ์ค์ผ ํจ.
// e๋ ๋ฐ์ดํฐ(element), i๋ ๋ฒํธ(index)
quizInfo.forEach((e, i) => {
quizType[i].textContent = quizInfo[i].answerType;
quizNumber[i].textContent = "0" + quizInfo[i].answerNum + ". ";
quizAsk[i].textContent = quizInfo[i].answerAsk;
quizResult[i].textContent = quizInfo[i].answerResult;
});
๋ฌธ์ ์ถ๋ ฅ ๋ฐฉ๋ฒ์ ์์์ ์ธ ๋ฐฉ๋ฒ๊ณผ for๋ฌธ, forEach๋ฌธ, ์ด๋ ๊ฒ ์ด 3๊ฐ์ง์ ๋ฐฉ๋ฒ์ด ์๋ค.
forEach๋ฌธ์ด ๊ฐ์ฅ ๊ฐ๋ตํ๋ ๋ฐฉ๋ฒ์ผ๋ก ์ต์ข
์ ์ผ๋ก๋ ์์์ ๋ฐฉ๋ฒ๊ณผ for๋ฌธ์ ์ฃผ์์ฒ๋ฆฌ ํ forEach๋ฌธ์ ์ฌ์ฉํ๋ค.
// ์ ๋ต ์จ๊ธฐ๊ธฐ : ์์์ ๋ฐฉ๋ฒ
// quizResult[0].style.display = "none";
// quizResult[1].style.display = "none";
// quizResult[2].style.display = "none";
// quizResult[3].style.display = "none";
// ์ ๋ต ์จ๊ธฐ๊ธฐ : for๋ฌธ
// for(let i = 0; i < quizInfo.length; i++) {
// quizResult[i].style.display = "none";
// }
// ์ ๋ต ์จ๊ธฐ๊ธฐ : ์ฝ์์ฐฝ์ผ๋ก ํ
์คํธ
// quizInfo.forEach((element) => {
// console.log(element);
// });
// ์ ๋ต ์จ๊ธฐ๊ธฐ : forEach
quizInfo.forEach((e, i) => {
quizResult[i].style.display = "none";
});
๋ฌธ์ ๋ฅผ ์ถ๋ ฅํ๋ค๋ฉด ์ผ๋จ ์ ๋ต ํ์ธ ๋ฒํผ์ ๋๋ฅด๊ธฐ ์ ๊น์ง ๋ณด์ด๊ฒ ํด์๋ ์๋๋ค.
์ ๋ต ์จ๊ธฐ๋ ๋ฐฉ๋ฒ๋ ์์์ ๋ฐฉ๋ฒ๊ณผ for๋ฌธ, forEach ์ด๋ ๊ฒ 3๊ฐ์ง๊ฐ ์์ผ๋ฉฐ ์ต์ข
์ ์ผ๋ก๋ forEach๋ฅผ ์ฌ์ฉํ๋ค.
// ์ ๋ต ํ์ธ : forEach
// ์์ btn์ ๋ง๋ค์ด์, ์ ํ์ quizConfirm ๊ณผ ์ฐ๊ฒฐ์ํด
// ํ์
๊ดํธ ์์ ์์๋ ๋ฐ๋์ (element, index, array)์ด๊ธฐ๋๋ฌธ์ ์๋์ผ๋ก btn=element, num=index๊ฐ ๋จ.
quizConfirm.forEach((btn, num) => {
btn.addEventListener("click", () => { // ํด๋ฆญ ์ ์ด๋ฒคํธ ๋ฐ์ ์ํค๊ธฐ
// ์ฌ์ฉ์ ์ ๋ต
const userWord = quizInput[num].value; // ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋ต์ userWord๋ผ๋ ์์์ ์ ์ฅ!
// ์ฌ์ฉ์ ์ ๋ต == ๋ฌธ์ ์ ๋ต (๋ง๋์ง ํ์ธ)
if(userWord == quizInfo[num].answerResult) {
//์ ๋ต์ด๋ฉด
// alert("์ ๋ต"); // alert ํ
์คํธ์ฉ, ํ
์คํธ ๋ง์น ํ ์ฃผ์์ฒ๋ฆฌ
quizView[num].classList.add("like"); // ๊ฐ์์ง ๋ฏธ์
quizConfirm[num].style.display = "none"; // ์ ๋ตํ์ธ๋ฒํผ ์จ๊น
quizResult[num].style.display = "none"; // ์ ๋ต ์จ๊ธฐ๊ธฐ
quizInput[num].style.display = "block"; // ์
๋ ฅ์ฐฝ ๋จ๊ธฐ๊ธฐ
} else {
//์ค๋ต์ด๋ฉด
// alert("์ค๋ต");
quizView[num].classList.add("dislike"); // ๊ฐ์์ง ์ฌํ
quizConfirm[num].style.display = "none"; // ์ ๋ตํ์ธ๋ฒํผ ์จ๊น
quizResult[num].style.display = "block"; // ์ ๋ต ํ์
quizInput[num].style.display = "none"; // ์
๋ ฅ์ฐฝ ์จ๊น
}
});
});
์ด์ ์ ๋ต ์
๋ ฅ ํ์ ๋์์ ๋ง๋ค์ด์ผ ํ๋ค. ์์ด ๋ณต์กํด์ง๊ธฐ ๋๋ฌธ์ ๋ฐ๋์ forEach๋ฌธ์ ์ฌ์ฉํด์ผ ํ ๊ฒ.
addEventListener๋ฅผ ํตํด ํด๋ฆญ ์์ ์ด๋ฒคํธ๋ฅผ, if๋ฌธ์ ํตํด ์ฌ์ฉ์์ ์
๋ ฅ ๋ฐ์ดํฐ์ ๋ฌธ์ ์ ๋ต์ ๋น๊ตํ๊ณ ๊ทธ ๋ค์
๋์์ผ ํ ๋์๋ค์ ์์ ์์ ์ฝ๋์ ๊ฐ์ด ๊ตฌํํ๋๋ก ํ๋ค.
๋๊ธ