Note - Double Click to Copy Code Contact Us!

Quiz-App using HTML CSS JS

Tech Doubility
Quiz-App using HTML CSS JS

HTML
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <link rel="stylesheet" href="style.css" />
  7. <title>Quiz App</title>
  8. </head>
  9. <body>
  10. <div class="quiz-container" id="quiz">
  11. <div class="quiz-header">
  12. <h2 id="question">Question text</h2>
  13. <ul>
  14. <li>
  15. <input type="radio" name="answer" id="a" class="answer">
  16. <label for="a" id="a_text">Question</label>
  17. </li>
  18.  
  19. <li>
  20. <input type="radio" name="answer" id="b" class="answer">
  21. <label for="b" id="b_text">Question</label>
  22. </li>
  23.  
  24. <li>
  25. <input type="radio" name="answer" id="c" class="answer">
  26. <label for="c" id="c_text">Question</label>
  27. </li>
  28.  
  29. <li>
  30. <input type="radio" name="answer" id="d" class="answer">
  31. <label for="d" id="d_text">Question</label>
  32. </li>
  33. </ul>
  34. </div>
  35. <button id="submit">Submit</button>
  36. </div>
  37. <script src="script.js"></script>
  38. </body>
  39. </html>
CSS
  1. @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400&display=swap');
  2.  
  3. * {
  4. box-sizing: border-box;
  5. }
  6.  
  7. body {
  8. background-color: #b8c6db;
  9. background-image: linear-gradient(315deg, #b8c6db 0%, #f5f7fa 100%);
  10. font-family: 'Poppins', sans-serif;
  11. display: flex;
  12. align-items: center;
  13. justify-content: center;
  14. height: 100vh;
  15. overflow: hidden;
  16. margin: 0;
  17. }
  18.  
  19. .quiz-container {
  20. background-color: #fff;
  21. border-radius: 10px;
  22. box-shadow: 0 0 10px 2px rgba(100, 100, 100, 0.1);
  23. width: 600px;
  24. overflow: hidden;
  25. }
  26.  
  27. .quiz-header {
  28. padding: 4rem;
  29. }
  30.  
  31. h2 {
  32. padding: 1rem;
  33. text-align: center;
  34. margin: 0;
  35. }
  36.  
  37. ul {
  38. list-style-type: none;
  39. padding: 0;
  40. }
  41.  
  42. ul li {
  43. font-size: 1.2rem;
  44. margin: 1rem 0;
  45. }
  46.  
  47. ul li label {
  48. cursor: pointer;
  49. }
  50.  
  51. button {
  52. background-color: #8e44ad;
  53. color: #fff;
  54. border: none;
  55. display: block;
  56. width: 100%;
  57. cursor: pointer;
  58. font-size: 1.1rem;
  59. font-family: inherit;
  60. padding: 1.3rem;
  61. }
  62.  
  63. button:hover {
  64. background-color: #732d91;
  65. }
  66.  
  67. button:focus {
  68. outline: none;
  69. background-color: #5e3370;
  70. }
JavaScript
  1. const quizData = [
  2. {
  3. question: "Which language runs in a web browser?",
  4. a: "Java",
  5. b: "C",
  6. c: "Python",
  7. d: "JavaScript",
  8. correct: "d",
  9. },
  10. {
  11. question: "What does CSS stand for?",
  12. a: "Central Style Sheets",
  13. b: "Cascading Style Sheets",
  14. c: "Cascading Simple Sheets",
  15. d: "Cars SUVs Sailboats",
  16. correct: "b",
  17. },
  18. {
  19. question: "What does HTML stand for?",
  20. a: "Hypertext Markup Language",
  21. b: "Hypertext Markdown Language",
  22. c: "Hyperloop Machine Language",
  23. d: "Helicopters Terminals Motorboats Lamborginis",
  24. correct: "a",
  25. },
  26. {
  27. question: "What year was JavaScript launched?",
  28. a: "1996",
  29. b: "1995",
  30. c: "1994",
  31. d: "none of the above",
  32. correct: "b",
  33. },
  34. ];
  35.  
  36. const quiz = document.getElementById('quiz')
  37. const answerEls = document.querySelectorAll('.answer')
  38. const questionEl = document.getElementById('question')
  39. const a_text = document.getElementById('a_text')
  40. const b_text = document.getElementById('b_text')
  41. const c_text = document.getElementById('c_text')
  42. const d_text = document.getElementById('d_text')
  43. const submitBtn = document.getElementById('submit')
  44.  
  45. let currentQuiz = 0
  46. let score = 0
  47.  
  48. loadQuiz()
  49.  
  50. function loadQuiz() {
  51. deselectAnswers()
  52.  
  53. const currentQuizData = quizData[currentQuiz]
  54.  
  55. questionEl.innerText = currentQuizData.question
  56. a_text.innerText = currentQuizData.a
  57. b_text.innerText = currentQuizData.b
  58. c_text.innerText = currentQuizData.c
  59. d_text.innerText = currentQuizData.d
  60. }
  61.  
  62. function deselectAnswers() {
  63. answerEls.forEach(answerEl => answerEl.checked = false)
  64. }
  65.  
  66. function getSelected() {
  67. let answer
  68.  
  69. answerEls.forEach(answerEl => {
  70. if(answerEl.checked) {
  71. answer = answerEl.id
  72. }
  73. })
  74.  
  75. return answer
  76. }
  77.  
  78. submitBtn.addEventListener('click', () => {
  79. const answer = getSelected()
  80.  
  81. if(answer) {
  82. if(answer === quizData[currentQuiz].correct) {
  83. score++
  84. }
  85.  
  86. currentQuiz++
  87.  
  88. if(currentQuiz < quizData.length) {
  89. loadQuiz()
  90. } else {
  91. quiz.innerHTML = `
  92. <h2>You answered ${score}/${quizData.length} questions correctly</h2>
  93.  
  94. <button onclick="location.reload()">Reload</button>
  95. `
  96. }
  97. }
  98. })

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.