Quiz 객관식(여러문제) 확인하기 : 점수

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
kimlh3743@gmail.com
소스 보기
QuizEffect01
JAVASCRIPT HTML CSS
//문제 정보
                                        const quizInfo = [
                                            {
                                                answerType: "웹디자인기능사 2011년 1회",
                                                answerNum: "1",
                                                answerAsk: "건강 이미지의 웹사이트를 구성하려고 한다. 가장 적합한 컬러는?",
                                                answerChoice: {
                                                    1: "빨강색",
                                                    2: "노랑색",
                                                    3: "검정색",
                                                    4: "녹색",
                                                },
                                                answerResult: "4",
                                                answerEx: "초록은 싱그러움과 맑음 등을 상징하기에 적합합니다."
                                            },
                                            .
                                            .
                                            .
                                            {
                                                answerType: "웹디자인기능사 2011년 1회",
                                                answerNum: "60",
                                                answerAsk: "시각 디자인 영역의 한 영역이며 글자체, 글자크기, 행간, 여백, 간격, 단락, 그리드 등을 통해 전달되는 것은 무엇에 대한 활용인가?",
                                                answerChoice: {
                                                    1: "내비게이션 디자인(Navigation Design)",
                                                    2: "웹 타이포그래피 디자인(Web Typographic Design)",
                                                    3: "웹 컬러 디자인(Web Color Design)",
                                                    4: "웹 인터페이스 디자인(Web Interface Design)",
                                                },
                                                answerResult: "2",
                                                answerEx: "다른 글자와 구별되는 그 글자의 형태를 '폰트'(서체) 라고 하며, 글자와 글자가 위치한 공간, 레이아웃된 모양 등 글자로 디자인된 상태를 모두 폭 넓게 타이포그라피라고 정의합니다."
                                            }
                                        ];
                                
                                        const quizWrap = document.querySelector(".quiz__wrap");
                                        let quizScore = "";
                                
                                        //문제 출력
                                        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>
                                            `);          //push를 이용해서 내용을 넣는다. 
                                
                                            });
                                
                                            exam.push(`
                                                <div class="quiz__confirm">
                                                    <button class="check">정답 확인하기</button>
                                                    <div class="ex">
                                                    </div>
                                                </div>
                                            `)
                                
                                            quizWrap.innerHTML = exam.join('');     //join으로 중간에 생기는 따옴표 지운다.
                                        }
                                        updateQuiz();
                                
                                        //정답 확인
                                        const answerQuiz = () => {      //정답 확인 버튼 클릭시의 이벤트
                                            const quizSelects = document.querySelectorAll(".quiz__selects");        //객관식 보기
                                            // const quizEx = document.querySelector(".quiz__confirm .ex");     //체크한 보기 == 문제 정답 확인
                                
                                            //사용자 체크한 보기 == 문제 정답
                                            quizInfo.forEach((question, number) => {
                                                const userSelector = `input[name=select${number}]:checked`; //사용자가 체크한 것   (이해가 잘)
                                                const quizSelectsWrap = quizSelects[number];                //0은 첫번째 문제, 1은 두번째 문제... quizSelectsWrap으로 정해준 것
                                                const userAnswer = (quizSelectsWrap.querySelector(userSelector) || {}).value;           //quiz__selects[number] 체크 된 것을 찾는다. | {} 빈 문자열은 체크 하지 않을 경우의 출력
                                                const quizView = document.querySelectorAll(".quiz__view")       //강아지
                                
                                                console.log(userAnswer)
                                
                                                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>`
                                                }
                                            });
                                
                                            //전체 문제수 
                                            // console.log(quizInfo.length)
                                
                                            //내가 맞힌 수
                                            // console.log(quizScore)      //콘솔로그 응용
                                
                                            //정답 수 / 합격여부 출력
                                
                                            // quizEx.innerText = `전체 갯수 : ${quizInfo.length} 맞은 갯수 : ${quizScore}`;
                                
                                            if (36 <= quizScore) {
                                                document.querySelector(".ex").textContent = "전체" + quizInfo.length + "개 중 " + quizScore + "개 정답으로, 합격."
                                            } else {
                                                document.querySelector(".ex").textContent = "전체" + quizInfo.length + "개 중 " + quizScore + "개 정답으로, 불합격."
                                            }
                                
                                        }
                                
                                        //정답 클릭
                                        document.querySelector(".quiz__confirm .check").addEventListener("click", answerQuiz)
<div class="quiz__wrap">
    <div class="quiz">
        <span class="quiz__type"></span>
        <h2 class="quiz__question">
            <span class="number"></span>
            <div class="ask"></div>
        </h2>
        <div class="quiz__view">
            <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">
            <button class="confirm">정답 확인하기</button>
            <div class="result"></div>
        </div>
    </div>
</div>
.quiz__wrap {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    margin-top: 50px;
    margin-bottom: 150px;
    flex-wrap: wrap;
}
.quiz {
    max-width: 500px;
    width: 100%;
    background-color: #fff;
    border: 8px ridge #cacaca;
    margin: 10px;
}
.quiz__type {
    background-color: #cacaca;
    text-align: center;
    display: block;
    font-size: 16px;
    border: 3px ridge #cacaca;
    color: #3b3b3b;
    font-family: "DungGeunMo";
    padding: 4px;
}
.quiz__question {
    border-top: 6px ridge #cacaca;
    border-bottom: 6px ridge #cacaca;
    padding: 20px;
    font-family: "Wooridaum";
    line-height: 1.3;
}
.quiz__question .number {
    color: rgb(187, 87, 87);
}
.quiz__question .ask {
    display: inline;
}
.quiz__answer {
    border-top: 6px ridge #cacaca;
    padding: 10px;
    background-color: #f5f5f5;
}
.quiz__answer .confirm {
    border: 6px ridge #cacaca;
    width: 100%;
    font-size: 22px;
    padding: 13px 20px;
    background-color: #b6b6b6;
    font-family: "Wooridaum";
    cursor: pointer;
}
.quiz__answer .result {
    width: 100%;
    font-size: 22px;
    line-height: 1.4;
    padding: 13px 20px;
    border: 6px ridge #cacaca;
    box-sizing: border-box;
    text-align: center;
    font-family: "Wooridaum";
}
.quiz__answer .input {
    width: 100%;
    border: 6px ridge #cacaca;
    font-size: 22px;
    padding: 13px 20px;
    background-color: #fff;
    font-family: "Wooridaum";
    margin-bottom: 10px;  
}
.quiz__view {
    background-color: #f5f5f5;
    font-family: "Wooridaum";
    position: relative;
    overflow: hidden;
}
.quiz__view .true {
    width: 120px;
    height: 120px;
    line-height: 120px;
    background-color:  rgb(187, 87, 87);
    color: #fff;
    border-radius: 50%;
    text-align: center;
    position: absolute;
    left: 70%;
    top: 100px;
    opacity: 0;
}
.quiz__view .false {
    width: 120px;
    height: 120px;
    line-height: 120px;
    background-color:  #fff;
    border-radius: 50%;
    text-align: center;
    position: absolute;
    right: 70%;
    top: 100px;
    opacity: 0;
}
.quiz__view.like .true {
    opacity: 1;
    animation: wobble 0.6s;
}
.quiz__view.dislike .false {
    opacity: 1;
    animation: wobble 0.6s;
}
.quiz__selects {
    margin: 5px 0;
}
.quiz__selects label {
    display: flex;
}
.quiz__selects label input {
    position: absolute;
    left: -9999px;
}
.quiz__selects label span {
    font-size: 20px;
    line-height: 1.3;
    font-family: "Wooridaum";
    padding: 10px;
    display: flex;
    align-items: center;
    width: 100%;
    border-radius: 5px;
    cursor: pointer;
}
.quiz__selects label span::before{
    content: '';
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: #fff;
    margin-right: 15px;
    display: flex;
    flex-shrink: 0;
    box-shadow: inset 0px 0px 0px 4px rgb(187, 87, 87);
    transition: all 0.25s;
}
.quiz__selects label input:checked + span {
    background-color: rgb(244, 202, 202);
}
.quiz__selects label input:checked + span::before {
    box-shadow: inset 0px 0px 0px 10px rgb(187, 87, 87);
}
.quiz__confirm {
    width: 100%;
    text-align: center;
}
.quiz__confirm .check {
    font-size: 22px;
    line-height: 1.3;
    padding: 13px 60px;
    border: 6px ridge #cacaca;
    box-sizing: border-box;
    text-align: center;
    font-family: "Wooridaum";
    cursor: pointer;
    margin: 40px 0;
    transition: background 0.3s;
}
.quiz__confirm .check:hover {
    background: rgba(187, 87, 87, 0.263)
}
@keyframes wobble {
    0%   {transform: translateX(0) rotate(0deg)}
    15%  {transform: translateX(-25%) rotate(-5deg)}
    30%  {transform: translateX(20%) rotate(3deg)}
    45%  {transform: translateX(-15%) rotate(-3deg)}
    60%  {transform: translateX(10%) rotate(2deg)}
    75%  {transform: translateX(-5%) rotate(-1deg)}
    100% {transform: translateX(0) rotate(0deg)}
}