๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
EFFECT

ํ€ด์ฆˆ ๋ฌธ์ œ ๋งŒ๋“ค๊ธฐ03

by _ํ† ๋งคํ†  2022. 8. 6.
728x90

 

 

ํ€ด์ฆˆ ์ฃผ๊ด€์‹(์—ฌ๋Ÿฌ๋ฌธ์ œ) ์œ ํ˜•

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ ํ•„๊ธฐ๋ฌธ์ œ๋ฅผ ์ฃผ๊ด€์‹ ์œ ํ˜• ํ€ด์ฆˆ๋กœ ์—ฌ๋Ÿฌ๊ฐœ ์ œ์ž‘ํ•ด๋ณด๊ธฐ
4๊ฐœ์˜ ์ฃผ๊ด€์‹ ํ€ด์ฆˆ๋ฅผ ํ•˜๋“œ ์ถœ๋ ฅ, ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•œ ์ถœ๋ ฅ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์‚ดํŽด๋ณด์ž!


#1. ์„ ํƒ์ž ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค

์ƒ์ˆ˜์—๊ฒŒ ํ•ด๋‹น๋ช…์นญ(ex: quizType)์„ ์„ ์–ธํ•ด์ฃผ๊ณ  ์ฟผ๋ฆฌ์…€๋ž™ํ„ฐ(ํƒœ๊ทธ, ์•„์ด๋””, ํด๋ž˜์Šค๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ)๋กœ ์„ ํƒํ•œ ์š”์†Œ๋ฅผ ์ฐพ์•„์„œ ๋„ฃ์–ด๋ณด์ž!

//์„ ํƒ์ž
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"); //๊ฐ•์•„์ง€

=> ํ€ด์ฆˆ ๋ฌธ์ œ ๋งŒ๋“ค๊ธฐ 02๋ฒˆ์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ํ•˜๋‚˜์˜€๊ธฐ ๋•Œ๋ฌธ์— querySelector๋งŒ ์‚ฌ์šฉํ•ด์ฃผ์—ˆ์ง€๋งŒ ์ด๋ฒˆ ์Šคํฌ๋ฆฝํŠธ๋Š” ํ€ด์ฆˆ ๋ฌธ์ œ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ด๋ฏ€๋กœ querySelector ๋’ค์— All์„ ๋ถ™์—ฌ์„œ ๋ชจ๋“  ์„ ํƒ์ž๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋„๋ก ํ•œ๋‹ค.

<๊ฒฐ๊ณผ ๋ณด๊ธฐ>


#2. ๋ฌธ์ œ ์ •๋ณด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค

๋ฐฐ์—ด ์•ˆ ๊ฐ์ฒด๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์—ฌ ํ‚ค์™€ ๊ฐ’์œผ๋กœ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฌธ์ œ์ •๋ณด(๋ฐ์ดํ„ฐ)๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ €์žฅ์‹œํ‚จ๋‹ค.

//๋ฌธ์ œ์ •๋ณด
const quizInfo = [
    {
        answerType : "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2016๋…„01์›”24์ผ",
        answerNum : "1",
        answerAsk : "ํ•œ๊ตญ์‚ฐ์—…ํ‘œ์ค€(KS)์— ๋”ฐ๋ฅธ ์ƒ‰์˜ 3์†์„ฑ์€ ๋ฌด์—‡์ธ๊ฐ€?",
        answerResult : "Hue(์ƒ‰์ƒ),Value(๋ช…๋„),Chroma(์ฑ„๋„)"
    }, //0(index)
    {
        answerType : "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2016๋…„01์›”24์ผ",
        answerNum : "2",
        answerAsk : "๋ฏธ์  ๋Œ€์ƒ์„ ๊ตฌ์ƒํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ๋ถ€๋ถ„์˜ ์‚ฌ์ด์— ์งˆ์ ์œผ๋กœ๋‚˜ ์–‘์ ์œผ๋กœ ๋ชจ์ˆœ๋˜๋Š” ์ผ ์—†์ด ์งˆ์„œ๊ฐ€ ์žกํ˜€ ์–ด์šธ๋ฆฌ๋Š” ๊ฒƒ์€ ๋ฌด์—‡์ธ๊ฐ€?",
        answerResult : "์กฐํ™”"
    },//1
    {
        answerType : "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2016๋…„01์›”24์ผ",
        answerNum : "3",
        answerAsk : "๊ฐ™์€ ํฌ๊ธฐ์˜ ํ˜•์„ ์ƒ, ํ•˜๋กœ ๊ฒน์น  ๋•Œ ์œ„์ชฝ์˜ ๊ฒƒ์ด ํฌ๊ฒŒ ๋ณด์ด๋Š” ์ฐฉ์‹œํ˜„์ƒ์€ ๋ฌด์—‡์ธ๊ฐ€?",
        answerResult : "์ƒ๋ฐฉ ๊ฑฐ๋ฆฌ์˜ ๊ณผ๋Œ€์ฐฉ์‹œ"
    },//2
    {
        answerType : "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2016๋…„01์›”24์ผ",
        answerNum : "4",
        answerAsk : "์ƒ‰๊ด‘์˜ ํ˜ผํ•ฉ์—์„œ ์ƒ‰์„ ํ˜ผํ•ฉํ•˜๋ฉด ํ• ์ˆ˜๋ก ๋†’์•„์ง€๋Š” ์ƒ‰์˜ ์†์„ฑ์€ ๋ฌด์—‡์ธ๊ฐ€?",
        answerResult : "๋ช…๋„"
    }//3
    //arr์€ ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•˜์ง€๋งŒ ํ˜„์žฌ ๋ฐฐ์—ด์•ˆ '๊ฐ์ฒด'๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด์˜ ํ‚ค ๊ฐ’์„ ๊ตฌ์ฒด์ ์œผ๋กœ ์„ ํƒํ•˜์—ฌ ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    //์ฃผ์˜ : ํ‚ค๋ฅผ ์„ ํƒํ•˜์ง€ ์•Š์€ ์ฑ„๋กœ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด ๋ฌด์—‡์„ ์ถœ๋ ฅํ•˜๋ผ๋Š” ๊ฑด์ง€ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜์—ฌ object๋กœ ๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค.
]

#3. ๋ฌธ์ œ ์ถœ๋ ฅํ•˜๊ธฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค

ํ•˜๋“œ ์ถœ๋ ฅ ๋ฐฉ๋ฒ•๊ณผ for๋ฌธ์„ ์ด์šฉํ•œ ์ถœ๋ ฅ๋ฐฉ๋ฒ•, ๊ทธ๋ฆฌ๊ณ  forEach๋ฌธ์„ ์‚ฌ์šฉํ•œ ์ถœ๋ ฅ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.
๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ ์›น์—์„œ ๋ฐ˜๋ณต๋ฌธ์ด ์–ด๋–ป๊ฒŒ ํ™œ์šฉ๋˜๊ณ  ์‘์šฉ๋˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž.

// ํ•˜๋“œ ์ถœ๋ ฅ
 quizType[0].textContent = quizInfo[0].answerType;
 quizType[1].textContent = quizInfo[1].answerType;
 quizType[2].textContent = quizInfo[2].answerType;
 quizType[3].textContent = quizInfo[3].answerType;
 // quizType
 quizNumber[0].textContent = quizInfo[0].answerNum + ". ";
 quizNumber[1].textContent = quizInfo[1].answerNum + ". ";
 quizNumber[2].textContent = quizInfo[2].answerNum + ". ";
 quizNumber[3].textContent = quizInfo[3].answerNum + ". ";
 // quizNumber
 quizAsk[0].textContent = quizInfo[0].answerAsk;
 quizAsk[1].textContent = quizInfo[1].answerAsk;
 quizAsk[2].textContent = quizInfo[2].answerAsk;
 quizAsk[3].textContent = quizInfo[3].answerAsk;
 // quizAsk
 quizResult[0].textContent = quizInfo[0].answerResult;
 quizResult[1].textContent = quizInfo[1].answerResult;
 quizResult[2].textContent = quizInfo[2].answerResult;
 quizResult[3].textContent = quizInfo[3].answerResult;
  // quizResult

<for๋ฌธ์œผ๋กœ ๋ฌธ์ œ ์ถœ๋ ฅํ•˜๊ธฐ>

๋ฐ˜๋ณต์ ์ธ ๊ฒƒ๋“ค์€ ๋ฐ˜๋ณต๋ฌธ์ธ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ํšจ์œจ์ ์œผ๋กœ text ์‚ฝ์ž…์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
.length๋ฅผ ์‚ฌ์šฉํ•œ for๋ฌธ : i๊ฐ€ 0์ผ ๋•Œ quizInfo ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ปดํ“จํ„ฐ๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ ๋ฐฐ์—ด ๊ธธ์ด์ธ 3๊นŒ์ง€ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

for(let i=0; i<quizInfo.length; i++ ){
    quizType[i].textContent = quizInfo[i].answerType;
    quizNumber[i].textContent = quizInfo[i].answerNum + ". ";
    quizAsk[i].textContent = quizInfo[i].answerAsk;
    quizResult[i].textContent = quizInfo[i].answerResult;
}
for๋ฌธ ์ •๋ฆฌ
quizType[i].textContent = quizInfo[i].answerType;
quizType ์ƒ์ˆ˜์— ์„ ์–ธํ•œ ํƒœ๊ทธ์— quizInfo ์ƒ์ˆ˜์˜ ๋ฐฐ์—ด ์† answerType ํ‚ค์™€ "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2016๋…„01์›”24์ผ" ๊ฐ’์„ ๋Œ€์ž…ํ•˜์—ฌ ์ถœ๋ ฅ
์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด = ๊ธฐ์ค€ ์˜ค๋ฅธ์ชฝ์„ ์™ผ์ชฝ์— ๋Œ€์ž…ํ•˜๋Š” ๊ฒƒ.
=> quizInfo[i] ๊ฐ’์ธ answerType์„ quizType[i] ๊ฐ’์— textContent๋ฅผ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ๋Œ€์ž…ํ•œ๋‹ค.

<forEach๋ฌธ์œผ๋กœ ๋ฌธ์ œ ์ถœ๋ ฅํ•˜๊ธฐ>

// forEach()๋กœ ์ถœ๋ ฅํ•˜๊ธฐ  =  arr.forEach(function(){});
// quizInfo๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ธ๋ฑ์Šค๋กœ ์ถœ๋ ฅ = quizType์˜ ์ธ๋ฑ์Šค์— quizInfo์˜ ์ธ๋ฑ์Šค ์† .answerType๋งŒ ์ถœ๋ ฅ
quizInfo.forEach(function(el, i){
    quizType[i].textContent = quizInfo[i].answerType;
    quizNumber[i].textContent = quizInfo[i].answerNum + ". ";
    quizAsk[i].textContent = quizInfo[i].answerAsk;
    quizResult[i].textContent = quizInfo[i].answerResult;
});

// ํ™”์‚ดํ‘œํ•จ์ˆ˜
quizInfo.forEach((el, i) => {
    quizType[i].textContent = quizInfo[i].answerType;
    quizNumber[i].textContent = quizInfo[i].answerNum + ". ";
    quizAsk[i].textContent = quizInfo[i].answerAsk;
    quizResult[i].textContent = quizInfo[i].answerResult;
});

#4. ์ •๋‹ต ์ˆจ๊ธฐ๊ธฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค

๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ด 4๊ฐ€์ง€ ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ์ˆจ๊ฒจ๋ณด์ž!

// ์ •๋‹ต์ˆจ๊ธฐ๊ธฐ
// ์ผ๋ฐ˜ ์ถœ๋ ฅ์œผ๋กœ ์ •๋‹ต ์ˆจ๊ธฐ๊ธฐ
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";
}


// forEach๋ฌธ์œผ๋กœ ์ •๋‹ต ์ˆจ๊ธฐ๊ธฐ
// forEach๋Š” e > i > a ์ˆœ์„œ๋กœ ์‹์„ ์ฝ๋Š”๋‹ค.
// ์ฆ‰ ๋ฐ์ดํ„ฐ์—์„œ ์ธ๋ฑ์Šค๋ฅผ ๋ฝ‘๊ณ  ์‹ถ๋‹ค๋ฉด e ์™€ i๋ฅผ ๋ชจ๋‘ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋งŒ์•ฝ i๋งŒ ์„ ์–ธํ•ด๋„ i๋ฅผ e๋กœ ์ƒ๊ฐํ•˜์—ฌ ์ฝ๋Š”๋‹ค.
quizInfo.forEach((e, i)=>{
    quizResult[i].style.display = "none";
});
์ •๋ฆฌ
quizResult[i].style.display = "none";
๊ฐ ์ธ๋ฑ์Šค์˜ Result๊ฐ’์— css์Šคํƒ€์ผ ์ค‘ display = "none";์„ ์ฃผ์–ด์„œ ์ •๋‹ต ๊ฒฐ๊ณผ ๋ฐ•์Šค๋ฅผ ๋ณด์ด์ง€ ์•Š๊ฒŒ ํ•ด์ค€๋‹ค.

#5. ์ •๋‹ต ํ™•์ธํ•˜๊ธฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค

forEach๋ฌธ๊ณผ addEventListener๋ฅผ ํ†ตํ•ด ์ •๋‹ต์„ ํ™•์ธํ•ด๋ณด์ž! ๊ทธ๋ฆฌ๊ณ  classList๋ฅผ ํ†ตํ•ด ๋‹ต์— ๋”ฐ๋ฅธ ํ‘œ์ • ๋ณ€ํ™”๋ฅผ ์„ ์–ธํ•ด๋ณด์ž!

// ์ •๋‹ตํ™•์ธํ•˜๊ธฐ
// quizConfirm.addEventListener("click",() => {});
// quizConfirm.addEventListener("click",function(){});
// quizConfirm๋ฅผ ํด๋ฆญํ•  ์‹œ ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋˜๋„๋กํ•œ๋‹ค.

quizConfirm.forEach((btn, num)=>{
    btn.addEventListener("click", ()=>{
        
        // ์‚ฌ์šฉ์ž ์ •๋‹ต
        const userWord = quizInput[num].value; //์‚ฌ์šฉ์ž์˜ ์ •๋‹ต์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜์‹œํ‚ค๊ณ  ์—ฌ๋ฐฑ์„ ์—†์• ์ค€๋‹ค.
        
        // ์‚ฌ์šฉ์ž ์ •๋‹ต = ๋ฌธ์ œ ์ •๋‹ต
        if(userWord == quizInfo[num].answerResult){ 
            // num = ํŠน๋ณ„ํ•œ ๊ธฐ๋Šฅ์œผ๋กœ ๋„ฃ์€ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ธ๋ฑ์Šค๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…์นญ์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค. ( 0๋ฒˆ ๊ฐ์ฒด์˜ ํ€ด์ฆˆ 1๋ฒˆ ๊ฐ์ฒด์˜ ํ€ด์ฆˆ ๋“ฑ )
            // ์ •๋‹ต
            quizView[num].classList.add("like");
            quizConfirm[num].style.display = "none";
        } else {
            // ์˜ค๋‹ต
            quizView[num].classList.add("dislike");
            quizConfirm[num].style.display = "none";
            quizResult[num].style.display = "block";
            quizInput[num].style.display = "none"
            // ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ •๋‹ต๊ณผ ์˜ค๋‹ต์— ๊ฐ๊ฐ ์Šคํƒ€์ผ ์„ ์–ธ์„ ๋”ฐ๋กœ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
        }
    });
});

forEach์ •๋ฆฌ
1. quizConfirm์˜ ์ˆ˜๊ฐ€ 1๊ฐœ๊ฐ€ ์•„๋‹Œ 4๊ฐœ๋กœ ๋ณต์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— forEach๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ๊ฐ€ ์ด 4๊ฐœ๋ผ๋Š” ์ •๋ณด๋ฅผ ์„ ์–ธ ํ•ด์ฃผ์–ด์•ผ ์ •์ƒ์œผ๋กœ ์ด๋ฒคํŠธ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
2. quizConfirm.addEventListener("click",(btn, num) => {});์—์„œ btn == element / >num == index์ด๋ฉฐ ์ด๋ฆ„์„ ๋ถ™์—ฌ์ค€ ๊ฒƒ ๋ฟ, ํŠน๋ณ„ํ•œ ๊ธฐ๋Šฅ์ด ๊ฐ€๋ฏธ๋œ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.
3. quizConfirm == botten์ด๋ฉฐ ์ƒ์ˆ˜์—[num]์„ ๋ถ™์—ฌ ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ธ๋ฑ์Šค ๋ณ„๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ๋” ํ•œ๋‹ค.


!๊ธฐ์–ตํ•ด์•ผ ํ•  ๋ฌธ๋ฒ•!

๋ฐ์ดํ„ฐ ์ €์žฅ ๋ฐฉ๋ฒ• : ๋ฐฐ์—ด ์† ๊ฐ์ฒด ํƒ€์ž…
๊ฐ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ํšจ์œจ์ น์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

const quizInfo = [
    {ํ‚ค1 : ๊ฐ’1},{ํ‚ค2 : ๊ฐ’2},{ํ‚ค3 : ๊ฐ’3},{ํ‚ค4 : ๊ฐ’4}
];
//๊ฐ์ฒด๋Š” ํ•ญ์ƒ ํ‚ค์™€ ๊ฐ’(key-value pair)์œผ๋กœ ๊ฐ’์„ ๋„ฃ์–ด์•ผ ํ•˜๋ฉฐ ๊ทธ ๊ฐ’๋“ค์€ ์†์„ฑ(Property)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
//๊ฐ์ฒด ํ•œ์Œ์˜ ๊ตฌ๋ถ„์€ ์‰ผํ‘œ(comma)๋กœ ํ•ด์ค€๋‹ค.

for๋ฌธ์„ ํ†ตํ•œ ์ถœ๋ ฅ ๋ฐฉ๋ฒ•
์ถœ๋ ฅํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒฝ์šฐ ์ €์žฅ ๋ฐฉ๋ฒ•์ด ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆ๋Ÿฌ์˜ค๋ ค๋Š” ๋ฐ์ดํ„ฐ๊ฐ’์˜ ํ‚ค๋ฅผ ๊ตฌ์ฒด์ ์œผ๋กœ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
์ฆ‰ .answerType๊ณผ ๊ฐ™์€ ํ‚ค๋ฅผ ์„ ํƒํ•˜์ง€ ์•Š์€ ์ฑ„๋กœ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด ๋ฌด์—‡์„ ์ถœ๋ ฅํ•˜๋ผ๋Š” ๊ฑด์ง€ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜์—ฌ object๋กœ ๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค.

for(let i=0; i<4; i++ ){
    quizType[i].textContent = quizInfo[i].answerType; 
}

forEach์„ ํ†ตํ•œ ์ถœ๋ ฅ ๋ฐฉ๋ฒ•
๊ฐ์ฒด์˜ ์ธ๋ฑ์Šค ๋ณ„๋กœ ๋“ค์–ด์žˆ๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— e์™€ i๋ฅผ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ•˜๋ฉฐ, ์ƒ์ˆ˜์—๋„ ๋˜‘๊ฐ™์ด ์ธ๋ฑ์Šค๋ฅผ ๋ถ™์—ฌ์ฃผ์–ด์•ผ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค.

quizInfo.forEach(function(e, i){
    quizType[i].textContent = quizInfo[i].answerType;
});

quizConfirm๋ฅผ ํด๋ฆญํ•  ์‹œ ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋˜๋„๋กํ•œ๋‹ค.

quizConfirm.addEventListener("click",function(){});

4๊ฐœ์˜ quizConfirm๊ฐ€ ์žˆ์Œ์„ ์•Œ๋ ค์ฃผ๊ณ  ๋ฒ„ํŠผ๋“ค์˜ ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

quizConfirm.forEach((btn, num)=>{
    btn.addEventListener("click", ()=>{});
});

ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€์‹œํ‚จ๋‹ค.

.classList.add("ํด๋ž˜์Šค๋ช…");

๊ฒฐ๊ณผ๋ณด๊ธฐ

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css