๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ดํŽ™ํŠธ

๊ฐ๊ด€์‹ ์œ ํ˜• ์—ฌ๋Ÿฌ๋ฌธ์ œ

by Youcodein 2022. 8. 16.
728x90
๋ฐ˜์‘ํ˜•

๊ฐ๊ด€์‹ ์—ฌ๋Ÿฌ๋ฌธ์ œ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

const quizWrap = document.querySelector(".quiz__wrap");
const quizScore = 0;
const quizConfirm = document.querySelector(".quiz__confirm .check")



//๋ฌธ์ œ ์ถœ๋ ฅ
const updateQuiz = () => {
    const exam = [];

    quizInfo.forEach((question, number) => {
        exam.push(`<div class="quiz">
        <span class="quiz__type">${question.answerType}</span>
        <h2 class="quiz__question">
            <span class="number">${question.answerNum}. </span>
            <div class="ask">${question.answerAsk}
            </div>
        </h2>
        <div class="quiz__view">
            <div class="true">์ •๋‹ต์ž…๋‹ˆ๋‹ค!</div>
            <div class="false">ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค!</div>
            <div class="dog">
                <div class="head">
                    <div class="ears"></div>
                    <div class="face"></div>
                    <div class="eyes">
                        <div class="teardrop"></div>
                    </div>
                    <div class="nose"></div>
                    <div class="mouth">
                        <div class="tongue"></div>
                    </div>
                    <div class="chin"></div>
                </div>
                <div class="body">
                    <div class="tail"></div>
                    <div class="legs"></div>
                </div>
            </div>
        </div>
        <div class="quiz__answer">
            <div class="quiz__selects">
                <label for="select1${number}">
                    <input type="radio" id="select1${number}" class="select" name="select${number}" value="1">
                    <span class="choice">${question.answerChoice[1]}</span>
                </label>
                <label for="select2${number}">
                    <input type="radio" id="select2${number}" class="select" name="select${number}" value="2">
                    <span class="choice">${question.answerChoice[2]}</span>
                </label>
                <label for="select3${number}">
                    <input type="radio" id="select3${number}" class="select" name="select${number}" value="3">
                    <span class="choice">${question.answerChoice[3]}</span>
                </label>
                <label for="select4${number}">
                    <input type="radio" id="select4${number}" class="select" name="select${number}" value="4">
                    <span class="choice">${question.answerChoice[4]}</span>
                </label>
            </div>
        </div>
    </div>`);

    });

    exam.push(`
                <div class="quiz__confirm">
                        <button class="check">์ •๋‹ต ํ™•์ธํ•˜๊ธฐ</button>
                        <div class="ex"></div>
                <div>
    `)
    quizWrap.innerHTML = exam.join('');
}

updateQuiz();

//์ •๋‹ต ํ™•์ธ
const answerQuiz = () => {
    const quizSelects = document.querySelectorAll(".quiz__selects");    //๊ฐ๊ด€์‹ ๋ณด๊ธฐ
    const quizConfirm = document.querySelector(".quiz__confirm .check");


    //์‚ฌ์šฉ์ž๊ฐ€ ์ฒดํฌํ•œ ๋ณด๊ธฐ == ๋ฌธ์ œ ์ •๋‹ต
    quizInfo.forEach((question, number) => {
        const userSelector = `input[name=select${number}]:checked`;     //์‚ฌ์šฉ์ž๊ฐ€ ์ฒดํฌํ•œ ๋ณด๊ธฐ
        const quizSelectsWrap = quizSelects[number];
        const userAnswer = (quizSelectsWrap.querySelector(userSelector) || {}).value;

        const quizView = document.querySelectorAll(".quiz__view"); // ๊ฐ•์•„์ง€


        if (userAnswer == question.answerResult) {
            console.log("์ •๋‹ต")
            quizView[number].classList.add("like");
            quizScore++;
        } else {
            console.log("์˜ค๋‹ต")
            quizView[number].classList.add("dislike");
            const divBox = document.createElement("div");
            quizSelectsWrap.appendChild(divBox).innerHTML = `<p class="result">${question.answerEx}</p>`

        }

    });

    document.querySelector(".quiz__confirm .check").innerHTML = `${quizScore < 36 ? quizInfo.length + "/" + quizScore + " ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค. ๋” ๊ณต๋ถ€ํ•˜์„ธ์š”!" : quizInfo.lenght + "/" + quizScore + "์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค!"}`;

    // //์ ์ˆ˜์™€ ํ•ฉ๋ถˆํ•ฉ
    // document.querySelector("quiz__confirm.ex").innerHTML = `${quizScore < 36 ? quizInfo.length + "/" + quizScore +" ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค. ๋” ๊ณต๋ถ€ํ•˜์„ธ์š”!" : quizInfo.lenght + "/" + quizScore + "์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค!"}`;
    // quizConfirm.textContent = `์ด ${quizInfo.length} ๋ฌธ์ œ ์ค‘์— ${quizScore} ๋ฌธ์ œ๋ฅผ ๋งž์ถ”์—ˆ์Šต๋‹ˆ๋‹ค.`

    //์ „์ฒด ๋ฌธ์ œ ์ˆ˜
    console.log(quizInfo.length);

    //๋‚ด๊ฐ€ ๋งžํžŒ ์ˆ˜
    console.log(quizScore.length);
}

//์ •๋‹ต ํด๋ฆญ
document.querySelector(".quiz__confirm .check").addEventListener("click", answerQuiz);

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€