๐ŸŽจ quiz

kirin.logยท2020๋…„ 11์›” 28์ผ
2

๐Ÿช logic

  • ๊ฐ์ฒด์™€ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด '๋ฌธ์ œ๋ฒˆํ˜ธ-๋ฌธ์ œ-๋ณด๊ธฐ'๋ฅผ ์ƒ์„ฑ์ž๋กœ ์—ฌ๋Ÿฌ๊ฐœ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค
  • ๋ณด๊ธฐ๋ฅผ ํด๋ฆญํ•˜๊ณ  ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ •๋‹ต๊ณผ ์˜ค๋‹ต์—ฌ๋ถ€ ์ฐฝ๋„์šฐ๊ธฐ
  • ์ด ์ ์ˆ˜ ๊ฒฐ๊ณผ ํ•ฉ์‚ฐ์—์„œ ์ ์ˆ˜ ๊ตฌ๊ฐ„ ๋ณ„๋กœ ์„œ๋กœ ๋‹ค๋ฅธ output ๋„์ถœํ•˜๊ธฐ
๐ŸŽ html
  <section>
    <div class="grid">
        <h1>๐Ÿ™‹โ€โ™€๏ธ Quiz Quiz ๐Ÿ™‹โ€โ™‚๏ธ</h1>
        <div id="quiz">
           <p id="question"></p>
      
           <div class="buttons">
              <button class="btn"></button>
              <button class="btn"></button>
              <button class="btn"></button>
              <button class="btn"></button>
           </div>
        </div>
    </div>    
  </section>
๐ŸŽ  css


 *:focus {    /* ๊ธฐ๋ณธ์ ์œผ๋กœ focus์ ์šฉ๋˜์–ด ์žˆ์Œ */
    outline: none;
  }
 
.grid {
    padding: 25px 40px 30px;
    margin: 60px auto;
    width: 800px;
    background: #fff;
    border: 2px solid #eed0dc;
    border-radius: 20px;
    box-shadow: 5px 5px 5px #cbcbcb;
 }

 .grid h1 {
    color: #333;
    font-size: var(--font-large);
    text-align: center;
    padding-bottom: 15px;
    ;
 }

 #question {
    padding: 10px 2em;
    background: salmon;
    border-radius: 15px;
    font-size: 22px;
    color: #fff;
    height: 45px;
 }

 #quiz {
    text-align: center;
 }

 .buttons {
    padding: 30px 20px;
    border: 2px solid #eed0dc;
    border-radius: 20px;
 }

 .btn {
    margin: 10px 40px 20px 0;
    padding: 10px;
    width: 300px;
    height: 40px;
    font-size: 16px;
    color: #333;
    background: #eed0dc;
    border: 1px solid #ffe3ed;
    border-radius: 15px;
    cursor: pointer;
    transition: all .2s;
 }

 .btn:nth-child(2n) {   /* .btn ๊ทธ๋ฃน์—์„œ 2๋ฒˆ์งธ์— ์œ„์น˜ํ•˜๋Š” ๋ชจ๋“ ์š”์†Œ ์„ ํƒ (2x1=2๋ฒˆ์งธ, 2x2=4๋ฒˆ์งธ) */
    margin-right: 0;
 }

 .btn:hover {
    background: #c34c74;
    color: #fff;
 }

 .btn:active {
    text-decoration: none;
 }



 @media only screen and (max-width:  768px) {
    body{
        display: grid;
        grid-template-rows: 60px 500px 1fr;
    }
    .grid{
        width:600px;
    }
    .grid h1{
        font-size: 30px;
    }
    #question, .btn {
        font-size: 15px;
        margin: 0;
        margin-bottom:10px;
        padding-bottom:30px;
    }
    .buttons {
        padding: 10px;
    }
}
๐ŸŽ  js


"use strict";

/* ํ€ด์ฆˆ ์ ์ˆ˜, ๋ฌธ์ œ ๋ฐ์ดํ„ฐ, ๋ฌธ์ œ ๋ฒˆํ˜ธ๋„ ํ•˜๋‚˜์˜ ํ€ด์ฆˆ ์ •๋ณด ๊ฐ์ฒด๋กœ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์ƒ์„ฑ์ž ํ•จ์ˆ˜(function)๋กœ ์ •์˜ํ•˜๊ธฐ */

// ํ€ด์ฆˆ ์ •๋ณด ๊ฐ์ฒด
 function Quiz(questions) {
    this.score = 0;             // ์ ์ˆ˜
    this.questions = questions; // ๋ฌธ์ œ
    this.questionIndex = 0;     // ์ง„ํ–‰์ค‘์ธ ๋ฌธ์ œ ๋ฒˆํ˜ธ
 }



 // ์ •๋‹ต ํ™•์ธ ๋ฉ”์„œ๋“œ
 Quiz.prototype.correctAnswer = function(answer) {      //Quiz์˜ ์›ํ˜•(prototype)์˜ correctAnswer๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ํ•จ์ˆ˜ ์ •์˜ํ•ด์ค€๋‹ค
   return answer == this.questions[this.questionIndex].answer;  //this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ•จ์ˆ˜์˜ ๋ฌธ์ œ, ๋ฌธ์ œ๋ฒˆํ˜ธ, ์ •๋‹ต ๊ฐ’์ด answer๋กœ ๋ฆฌํ„ด
}


// ๋ฌธ์ œ ๊ฐ์ฒด(์ƒ์„ฑ์ž ํ•จ์ˆ˜)
 function Question(text, choice, answer) {         //Question์˜ ์›ํ˜•๊ณผ ๊ทธ ์•ˆ์— ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž… ๋งŒ๋“ค๊ธฐ
   this.text = text;      // ์งˆ๋ฌธ ํ…์ŠคํŠธ
   this.choice = choice;  // ์„ ํƒํ•  ๋‹ต๋“ค(๋ณด๊ธฐ)
   this.answer = answer;  // ์ •๋‹ต ์ •๋ณด
}
// Question ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž 'new Question' ์ƒ์„ฑ์ž ์ƒ์„ฑํ•˜๊ธฐ(์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ)
 var questions = [
    new Question('๋‹ค์Œ ์ค‘ ๊ณผ์ผ์ด ์•„๋‹Œ ๊ฒƒ์€?', ['ํŒŒ์ธ์• ํ”Œ', '๋”ธ๊ธฐ', 'ํ† ๋งˆํ† ', '์‚ฌ๊ณผ'], 'ํ† ๋งˆํ† '),   //text, choice, answer ์ˆœ์œผ๋กœ ๋‚ด์šฉ ๋„ฃ๊ธฐ
    new Question('๋‹ค์Œ ์ค‘ ํ‚ค๊ฐ€ 170cm ์ดํ•˜์ธ ์—ฌ๋ฐฐ์šฐ๋Š”?', ['Blake Lively', 'Julia Roberts', 'Gwyneth Kate Paltrow', 'Natalie Portman'], 'Natalie Portman'),
    new Question('๋‹ค์Œ ์ค‘ ๋ฐฐ์šฐ "์ด๋ณ‘ํ—Œ"์˜ ์ถœ์—ฐ์ž‘์ด ์•„๋‹Œ ๊ฒƒ์€?', ['๊ณต๋™๊ฒฝ๋น„๊ตฌ์—ญ JSA', '๋ˆ„๊ตฌ๋‚˜ ๋น„๋ฐ€์€ ์žˆ๋‹ค', '๊ทธ ํ•ด ์—ฌ๋ฆ„', '์‹ ์„ธ๊ณ„'], '์‹ ์„ธ๊ณ„'),
    new Question('๋‹ค์Œ CSS ์†์„ฑ ์ค‘ ๊ธ€์ž์˜ ๊ตต๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์†์„ฑ์€?', ['font-size', 'font-style', 'font-weight', 'font-variant'], 'font-weight'),
    new Question('๋‹ค์Œ ์ค‘ ์šฐ๋ฆฌ๋‚˜๋ผ ๋ณด๋ฌผ1ํ˜ธ๋Š”?', ['๋™๋Œ€๋ฌธ', '์„œ๋Œ€๋ฌธ', '๋‚จ๋Œ€๋ฌธ', '๋ถ๋Œ€๋ฌธ'], '๋™๋Œ€๋ฌธ'),
    new Question('๋‹ค์Œ ์ค‘ ๋Œ€ํ•œ๋ฏผ๊ตญ์—์„œ ๊ฐ€์žฅ ๋‚จ์ชฝ์— ์žˆ๋Š” ์„ฌ์€?', ['๋…๋„', '๋งˆ๋ผ๋„', '์ œ์ฃผ๋„', '์šธ๋ฆ‰๋„'], '๋งˆ๋ผ๋„'),
    new Question('๋‹ค์Œ ์ค‘ ํ‘œํ˜„๋ฒ•์ด ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š”?', ['์ด๊ฒƒ์€ ์†Œ๋ฆฌ ์—†๋Š” ์•„์šฐ์„ฑ', '์ฆ๊ฑฐ์šด ๋น„๋ช…', '์ฐฌ๋ž€ํ•œ ์Šฌํ””์˜ ๋ด„', '์ฃฝ์–ด๋„ ์•„๋‹ˆ ๋ˆˆ๋ฌผ ํ˜๋ฆฌ์˜ค๋ฆฌ๋‹ค'], '์ฃฝ์–ด๋„ ์•„๋‹ˆ ๋ˆˆ๋ฌผ ํ˜๋ฆฌ์˜ค๋ฆฌ๋‹ค')
 ];

 // ํ€ด์ฆˆ ๊ฐ์ฒด ์ƒ์„ฑ/ Quiz ์ƒ์„ฑ์ž ๋งŒ๋“ค๊ณ  quiz๋ผ๊ณ  ์ด๋ฆ„์ง“๊ธฐ (quiz๊ฐ์ฒด ์†์„ฑ ๋ณต์‚ฌ - score, questions, questionIndex)
 var quiz = new Quiz(questions);

 // ๋ฌธ์ œ ์ถœ๋ ฅ ํ•จ์ˆ˜
 function updateQuiz() {
    var question = document.getElementById('question');
    var idx = quiz.questionIndex + 1;         //๋ฌธ์ œ ์ˆœ์„œ , ์ด 7๋ฌธ์ œ๊ฐ€ 1๋ฒˆ๋ถ€ํ„ฐ 1์”ฉ ์ถ”๊ฐ€
    var choice = document.querySelectorAll('.btn');  //๋ณด๊ธฐ4๊ฐœ  

    // ๋ฌธ์ œ ์ถœ๋ ฅ
    question.innerHTML = '๋ฌธ์ œ' + idx + ') ' + quiz.questions[quiz.questionIndex].text;  //ํ˜„์žฌ ์ถœ์ œ๋˜๊ณ  ์žˆ๋Š” ๋ฌธ์ œ ํ…์ŠคํŠธ(๋ฐฐ์—ด)
.                                               
    // ๋ณด๊ธฐ ์ถœ๋ ฅ
    for (var i = 0; i < 7; i++) {
       choice[i].innerHTML = quiz.questions[quiz.questionIndex].choice[i];
    }
 }


 var btn = document.querySelectorAll('.btn');  //var idx๋Š” updateQuiz์•ˆ์— ์ง€์ •ํ•œ ๋ณ€์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ. ๋”ฐ๋ผ์„œ var btn๋ณ€์ˆ˜๋ฅผ ์ „์—ญ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜ ๋ฐ–์—์„œ ๋‹ค์‹œ ์„ ์–ธํ•ด์คŒ




 // ์ž…๋ ฅ ๋ฐ ์ •๋‹ต ํ™•์ธ ํ•จ์ˆ˜
 function checkAnswer(i) {           //checkAnswerํ•จ์ˆ˜์— btn[i]์™€ ์ด๋ฒคํŠธ๋ฅผ ์ •์˜ํ•จ์œผ๋กœ์„œ, ํด๋ฆญํ•˜๋ฉด ์ •๋‹ต(answer)๋Š” ์ฒดํฌํ•œ ๋ฒ„ํŠผ(btn)์ด ๋œ๋‹ค๊ณ  ํ•จ์ˆ˜ ์ •์˜
    btn[i].addEventListener('click', function() {
       var answer = btn[i].innerText;

       if (quiz.correctAnswer(answer)) {   //ํด๋ฆญํ•œ btn์ด ์ •๋‹ต(correctAnswer)์ผ ๊ฒฝ์šฐ,
          alert('์ •๋‹ต์ž…๋‹ˆ๋‹ค!');
          quiz.score++;
       } else {
          alert('ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค!');
       }

       if (quiz.questionIndex < quiz.questions.length - 1) {   //์ „์ฒด ํ€ด์ฆˆ ๊ธธ์ด๋Š” 7๊ฐœ์ธ๋ฐ, ๊ทธ๋Ÿผ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๋Š” ์–ด๋–ป๊ฒŒ?
          quiz.questionIndex++;
          updateQuiz();
       } else {
          result();
       }
    });
 }

 function result() {
    var quizDiv = document.getElementById('quiz');
    var per = parseInt((quiz.score * 100) / quiz.questions.length);
    var txt = '<h1>๊ฒฐ๊ณผ</h1>' + '<h2 id="score">๋‹น์‹ ์˜ ์ ์ˆ˜: ' + quiz.score + '/' + quiz.questions.length + '<br><br>' + per + '์ ' + '</h2>';
. 
    quizDiv.innerHTML = txt;



    // ์ ์ˆ˜๋ณ„ ๊ฒฐ๊ณผ ํ…์ŠคํŠธ
    if (per < 40) {
       txt += '<h2>๋” ๋ถ„๋ฐœํ•˜์„ธ์š”</h2>';
       quizDiv.innerHTML = txt;
    } else if (per >= 40 && per < 70 ) {     // &&์€ and๋ผ๋Š” ๋œป. ์–‘์ชฝ์ด ๋ชจ๋‘ ์ถฉ์กฑํ•ด์•ผ true. ๋ฐ˜๋Œ€๋Š” ||(or์ด๋ผ๋Š” ๋œป. ํ•œ์ชฝ๋งŒ ์ถฉ์กฑํ•ด๋„ true)
       txt += '<h2>๋ฌด๋‚œํ•œ ์ ์ˆ˜๋„ค์š”</h2>'
       quizDiv.innerHTML = txt;
    } else if (per >= 70) {
       txt += '<h2>ํ›Œ๋ฅญํ•ฉ๋‹ˆ๋‹ค</h2>'   
       quizDiv.innerHTML = txt;
    }
 }

 for (var i = 0; i < btn.length; i++) {
    checkAnswer(i);
 }

 updateQuiz();





 //footer_dark mode
      var Body = {
         setBackColor : function(color) {
         document.querySelector('body').style.backgroundColor=color;
         },
         setColor : function(color) {
         document.querySelector('body').style.color=color;
         }
      }
    
      var Link = {
         setColor : function (color){
         var alist=document.querySelectorAll('a');
         var i = 0;
         while(i < alist.length){
            alist[i].style.color=color;
            i=i+1;
         }
         }
      }
    
      function handler(self){
         var target=document.querySelector('body')
         if(self.value==='night') {
            Body.setBackColor('black');
            Body.setColor('white');
            self.value='day';
     
            Link.setColor('powderblue');
     
         } else {
            Body.setBackColor('white');
            Body.setColor('black');
            self.value='night';
     
            Link.setColor('white');
            }
         }
profile
boma91@gmail.com

0๊ฐœ์˜ ๋Œ“๊ธ€