Advertisement
MarkUa

Untitled

Jun 30th, 2019
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.87 KB | None | 0 0
  1. <html lang="en">
  2. <head>
  3.     <meta charset="UTF-8">
  4.     <title>Pass test</title>
  5.     <link href="js-quiz.css" rel="stylesheet">
  6.     <script src="js-quiz-questions.js"></script>
  7.     <script src="js-quiz.js"></script>
  8. </head>
  9. <style>
  10.     #quiz-wrap {
  11.         font-family: arial, sans-serif;
  12.     }
  13.  
  14.     /* Wrapper of each question */
  15.     #quiz-wrap .question {
  16.         padding: 15px;
  17.         margin: 10px;
  18.         background: #f2f2f2;
  19.         border: 3px solid #ccc;
  20.         border-radius: 10px;
  21.     }
  22.  
  23.     /* Question */
  24.     #quiz-wrap h1 {
  25.         font-size: 22px;
  26.         margin: 5px 0;
  27.     }
  28.  
  29.     /* Options */
  30.     #quiz-wrap label {
  31.         display: block;
  32.         width: 100%;
  33.         font-size: 20px;
  34.         margin-top: 10px;
  35.         color: #333;
  36.     }
  37.  
  38.     /* Submit Button */
  39.     #quiz-wrap input[type=submit] {
  40.         background: #972121;
  41.         color: #fff;
  42.         padding: 10px;
  43.         border: 0;
  44.         padding: 10px 20px;
  45.         margin-top: 10px;
  46.         font-size: 20px;
  47.     }
  48. </style>
  49. <script>
  50.     var myTest = null;
  51.     function f() {
  52.         //get from server
  53.         myTest = {
  54.         test_name: "About me",
  55.         test_author: "Borys Lahno",
  56.         test_questions: [
  57.             {
  58.                 question: "What is my name?",
  59.                 answers: [
  60.                     {1: "Borys"},
  61.                     {2: "Tom"},
  62.                     {3: "Nick"},
  63.                     {4: "Mark"}
  64.                 ],
  65.                 correct_answer: "1"
  66.             },
  67.             {
  68.                 question: "How old I am?",
  69.                 answers: [
  70.                     {1: "15"},
  71.                     {2: "22"},
  72.                     {3: "20"},
  73.                     {4: "25"},
  74.                     {5: "17"}
  75.                 ],
  76.                 correct_answer: "3"
  77.             },
  78.             {
  79.                 question: "What do I like to do?",
  80.                 answers: [
  81.                     {1: "Play football"},
  82.                     {2: "Play computer games"},
  83.                     {3: "Play the Guitar"},
  84.                 ],
  85.                 correct_answer: "2"
  86.             },
  87.             {
  88.                 question: "I have a sister?",
  89.                 answers: [
  90.                     {1: "Yes"},
  91.                     {2: "No"},
  92.                 ],
  93.                 correct_answer: "1"
  94.             }
  95.         ]
  96.     }
  97.     }
  98.     var quiz = {
  99.         draw: function () {
  100.             f();
  101.             var wrapper = document.getElementById("quiz-wrap");
  102.  
  103.  
  104.             for (var i = 0; i < myTest.test_questions.length; ++i) {
  105.  
  106.                var number = (i + 1);
  107.                var qwrap = document.createElement("div");
  108.                qwrap.classList.add("question");
  109.  
  110.  
  111.                var question = document.createElement("h1");
  112.                question.innerHTML = number + ") " + myTest.test_questions[i].question;
  113.                qwrap.appendChild(question);
  114.  
  115.  
  116.                for (var j = 0; j < myTest.test_questions[i].answers.length; ++j) {
  117.  
  118.                    var label = document.createElement("label");
  119.                    qwrap.appendChild(label);
  120.  
  121.  
  122.                    var option = document.createElement("input");
  123.                    option.type = "radio";
  124.                    option.value = (j + 1);
  125.                    option.required = true;
  126.                    option.classList.add("oquiz");
  127.  
  128.  
  129.                    option.name = "quiz-" + number;
  130.                    label.appendChild(option);
  131.  
  132.  
  133.                    var answerOfQuestion;
  134.                    switch (j + 1) {
  135.                        case 1:
  136.                            answerOfQuestion = myTest.test_questions[i].answers[j]["1"];
  137.                            break;
  138.                        case 2:
  139.                            answerOfQuestion = myTest.test_questions[i].answers[j]["2"];
  140.                            break;
  141.                        case 3:
  142.                            answerOfQuestion = myTest.test_questions[i].answers[j]["3"];
  143.                            break;
  144.                        case 4:
  145.                            answerOfQuestion = myTest.test_questions[i].answers[j]["4"];
  146.                            break;
  147.                        case 5:
  148.                            answerOfQuestion = myTest.test_questions[i].answers[j]["5"];
  149.                            break;
  150.                    }
  151.                    var otext = document.createTextNode(answerOfQuestion);
  152.                    label.appendChild(otext);
  153.                }
  154.  
  155.  
  156.                wrapper.appendChild(qwrap);
  157.            }
  158.  
  159.  
  160.            var submitbutton = document.createElement("input");
  161.            submitbutton.type = "submit";
  162.            wrapper.appendChild(submitbutton);
  163.            wrapper.addEventListener("submit", quiz.submit);
  164.        },
  165.  
  166.        submit: function (evt) {
  167.  
  168.            evt.preventDefault();
  169.            evt.stopPropagation();
  170.  
  171.            var selected = document.querySelectorAll(".oquiz:checked");
  172.  
  173.            var score = 0;
  174.            for (var index = 0; index < myTest.test_questions.length; index++) {
  175.                if (selected[index].value == myTest.test_questions[index].correct_answer) {
  176.                    score++;
  177.                }
  178.            }
  179.  
  180.            var total = selected.length;
  181.            var percent = score / total;
  182.  
  183.  
  184.            var html = "<h1>";
  185.             if (percent >= 0.7) {
  186.                 html += "WELL DONE!";
  187.             } else if (percent >= 0.4) {
  188.                 html += "NOT BAD!";
  189.             } else {
  190.                 html += "TRY HARDER!";
  191.             }
  192.             html += "</h1>";
  193.             html += "<div>You scored " + score + " out of " + total + ".</div>";
  194.             document.getElementById("quiz-wrap").innerHTML = html;
  195.         }
  196.     };
  197.  
  198.     window.addEventListener("load", quiz.draw);
  199. </script>
  200. <body>
  201. <form id="quiz-wrap"></form>
  202. </body>
  203. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement