Quiz

  • 1
  • 2
  • 3
  • 4
  • 5
HTML CSS JavaScript
<div class="quiz__wrap">
    <!-- javascript quiz -->
</div>
.quiz__wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 150px;
    }

    .quiz {
    max-width: 500px;
    width: 100%;
    background: #fff;
    border: 8px ridge #cacaca;
    flex-wrap: wrap;
    margin: 10px;
    }

    .quiz__Type {
    background: #cacaca;
    text-align: center;
    font-size: 14px;
    color: #3b3b3b;
    border: 3px ridge #cacaca;
    padding: 3px 0;
    position: relative;
    }

    .quiz__question {
    border-top: 6px ridge #cacaca;
    border-bottom: 6px ridge #cacaca;
    padding: 13px 30px;
    font-size: 24px;
    line-height: 1.4
    }

    .quiz__Type::before {
    content: '';
    position: absolute;
    left: 2px;
    top: 2px;
    width: 5px;
    height: 5px;
    border: 6px ridge #cacaca;
    }

    .quiz__Type::after {
    content: '';
    position: absolute;
    right: 2px;
    top: 2px;
    width: 5px;
    height: 5px;
    border: 6px ridge #cacaca;
    }

    .quiz__number {
    font-family: 'Cafe24Dangdanghae';
    color: #bababa;
    }

    .quiz__ask {
    font-family: 'Cafe24Dangdanghae';
    color: #000;

    }

    .quiz__view {
    background-color: #f5f5f5;
    position: relative;
    }

    .quiz__view .true {
    position: absolute;
    left: 70%;
    top: 100px;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    z-index: 100;
    line-height: 100px;
    text-align: center;
    color: #fff;
    background-color: #f5534f;
    opacity: 0;
    }

    .quiz__view .false {
    position: absolute;
    right: 70%;
    top: 100px;
    width: 100px;
    height: 100px;
    background-color: #fff;
    border-radius: 50%;
    z-index: 100;
    line-height: 100px;
    text-align: center;
    color: #f5534f;
    background-color: #fff;
    opacity: 0;
    }

    .quiz__answer {
    background-color: #f5f5f5;
    border-top: 6px ridge #cacaca;
    padding: 10px;
    }

    .quiz__confirm {
    border: 6px ridge #cacaca;
    width: 100%;
    font-size: 22px;
    padding: 13px 20px;
    background: #d6d6d6;
    text-shadow: 1px 1px 1px #fff;
    transition: all 0.3s;
    cursor: pointer;
    }

    .quiz__confirm:hover {
    background: #b3b3b3;
    }

    .quiz__result {
    border: 6px ridge #cacaca;
    width: 100%;
    font-size: 22px;
    padding: 13px 20px;
    background: #fff;
    text-align: center;
    text-shadow: 1px 1px 1px #fff;
    font-family: 'Cafe24Dangdanghae';
    display: none;
    color: #000;
    }

    .quiz__input {
    border: 6px ridge #cacaca;
    width: 100%;
    font-size: 22px;
    padding: 13px 20px;
    background: #fff;
    text-shadow: 1px 1px 1px #fff;
    font-family: 'Cafe24Dangdanghae';
    margin-bottom: 10px;
    }
                        
    const quizWrap = document.querySelector(".quiz__wrap")

    const quizInfo = [{
            answerType: "javascript",
            answerNum: 1,
            answerAsk: "객체 기반의 스크립트 프로그래밍 언어는 무엇입니까?",
            answerChoice: {
                1: "javascript",
                2: "html",
                3: "css",
                4: "jquery"
            },
            answerResult: "1",
            answerEx: "객체 기반의 스크립트 프로그래밍 언어는 javascript입니다."
        },
        {
            answerType: "html",
            answerNum: 1,
            answerAsk: "웹페이지를 표현하기 위한 마크업 언어는 무엇입니까?",
            answerChoice: {
                1: "javascript",
                2: "html",
                3: "css",
                4: "jquery"
            },
            answerResult: "2",
            answerEx: "객체 기반의 스크립트 프로그래밍 언어는 javascript입니다."
        },
        {
            answerType: "css",
            answerNum: 1,
            answerAsk: "웹페이지를 꾸미기 위한 프로그래밍 언어는 무엇입니까?",
            answerChoice: {
                1: "javascript",
                2: "html",
                3: "css",
                4: "jquery"
            },
            answerResult: "3",
            answerEx: "웹페이지를 꾸미기 위한 프로그래밍 언어는 css입니다."
        }
    ];
    
    //문제 출력
    function updataQuiz() {
        const html = [];
        quizInfo.forEach((quesiton, number) => {
            html.push(`<div class="quiz">
            <h2 class="quiz__Type">${quesiton.answerType}</h2>
            <h3 class="quiz__question">
                <span class="quiz__number">${quesiton.answerNum}</span>
                <sapn class="quiz__ask">${quesiton.answerAsk}</sapn>
            </h3>
            <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 class="quiz__answer">
                    <div class="quiz__selects">
                        <label for="select1${number}">
                            <input class="select" type="radio" id="select1${number}" name="select${number}" value="1">
                            <span class="choice">${quesiton.answerChoice[1]}</span>
                        </label>
                        <label for="select2${number}">
                            <input class="select" type="radio" id="select2${number}" name="select${number}" value="2">
                            <span class="choice">${quesiton.answerChoice[2]}</span>
                        </label>
                        <label for="select3${number}">
                            <input class="select" type="radio" id="select3${number}" name="select${number}" value="3">
                            <span class="choice">${quesiton.answerChoice[3]}</span>
                        </label>
                        <label for="select4${number}">
                            <input class="select" type="radio" id="select4${number}" name="select${number}" value="4">
                            <span class="choice">${quesiton.answerChoice[4]}</span>
                        </label>
                    </div>
                </div>
            </div>
        </div>`)
        })
    
        html.push(`
            <div>
                <button class="quiz__confirm">정답 확인하기</button>
                <div class="quiz__result"></div>
            </div>
        `)
        quizWrap.innerHTML = html.join(' ');
    }
    updataQuiz();
    
    function answerQuiz(){
        const quizSelects = document.querySelectorAll(".quiz__selects")  //보기 선택 박스
        const quizView = document.querySelectorAll(".quiz__view");
        const quizResult = document.querySelector(".quiz__result");
    
        let scoreCurrent = 0;
        
        quizInfo.forEach((quesiton,number) =>{
            const quizSelectsWrap = quizSelects[number];        //전체 보기 박스
            const userSelector = `input[name=select${number}]:checked`    //사용자가 클릭한 것
            const userAnswer = (quizSelectsWrap.querySelector(userSelector) || {}).value;     // 클릭한 것의 속성 값
            
            if(userAnswer == quesiton.answerResult){
                // console.log("정답")
                quizView[number].classList.add("like");
                scoreCurrent++;
            }else{
                // console.log("오답")
                quizView[number].classList.add("dislike");
                const div = document.createElement("div");
                quizSelectsWrap.appendChild(div).innerHTML = `<p class="quiz__ex">${quesiton.answerEx}</p>`
            }
        });
        quizResult.innerHTML = `${quizInfo.length} 문제 중에 ${scoreCurrent} 문제를 맞췄습니다.`
    }
    document.querySelector(".quiz__confirm").addEventListener("click", answerQuiz);


dnwls7738@naver.com