• We just launched and are currently in beta. Join us as we build and grow the community.

How to Build a Simple Quiz App with a Timer Using HTML, CSS, and JavaScript?

Trixy

Forum Architect
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
152
Likes
53
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Title: How to Build a Simple Quiz App with a Timer Using HTML, CSS, and JavaScript?

In this tutorial, we will create a simple Quiz Application with Timer using HTML, CSS, and JavaScript. This step-by-step guide aims to provide you with a strong foundation and a practical example to kickstart your journey in developing similar interactive applications. By the end of this tutorial, you'll have a functional quiz app with a countdown timer to enhance user engagement.

What Does the Quiz Application with Timer Do?

The Simple Quiz Application with Timer is a user-friendly and interactive tool designed to test knowledge with a time-bound challenge. This application presents users with a quiz containing 5 multiple-choice questions. Users must complete the quiz within a time limit of 2 minutes and 30 seconds. If the timer runs out, the application automatically displays the results, even if some questions are left unanswered.

As users select their answers, the application instantly checks their response. Correct answers are highlighted with a green background, while incorrect choices are marked with a red background. This immediate feedback enhances the learning experience, making the quiz engaging and informative.

Let's start the coding part!

Step 1: Creating a JSON File

First, let's create a new JSON file. This file will store the data for the quiz questions that will be presented to the users. The JSON structure consists of objects with the following keys:

  • id: A unique identifier for each question.
  • question: The text of the question to be displayed.
  • choices: An array of possible answers.
  • answer: The correct answer, represented by the index of the correct choice in the choices array.

Below is an example of a JSON data structure created for this tutorial to define the quiz questions and answers:

data.json



  1. [
  2. {
  3. "id"

    :

    1

    ,
  4. "question"

    :

    "Which of the following is a JavaScript framework?"

    ,
  5. "choices"

    :

    [

    "Django"

    ,

    "React"

    ,

    "Flask"

    ,

    "Ruby"

    ]

    ,
  6. "answer"

    :

    1
  7. }

    ,
  8. {
  9. "id"

    :

    2

    ,
  10. "question"

    :

    "What does HTML stand for?"

    ,
  11. "choices"

    :

    [

    "Hyper Text Markup Language"

    ,

    "High Transfer Markup Language"

    ,

    "Hyperlinks and Text Markup Language"

    ,

    "Hyper Transfer Machine Language"

    ]

    ,
  12. "answer"

    :

    0
  13. }

    ,
  14. {
  15. "id"

    :

    3

    ,
  16. "question"

    :

    "Which of the following is used to declare a variable in Python?"

    ,
  17. "choices"

    :

    [

    "var"

    ,

    "let"

    ,

    "def"

    ,

    "None of the above"

    ]

    ,
  18. "answer"

    :

    3
  19. }

    ,
  20. {
  21. "id"

    :

    4

    ,
  22. "question"

    :

    "What is the output of console.log(typeof null) in JavaScript?"

    ,
  23. "choices"

    :

    [

    "null"

    ,

    "undefined"

    ,

    "object"

    ,

    "string"

    ]

    ,
  24. "answer"

    :

    2
  25. }

    ,
  26. {
  27. "id"

    :

    5

    ,
  28. "question"

    :

    "Which of these is not a valid CSS property?"

    ,
  29. "choices"

    :

    [

    "color"

    ,

    "font-size"

    ,

    "text-align"

    ,

    "print-mode"

    ]

    ,
  30. "answer"

    :

    3
  31. }
  32. ]

Step 2: Creating the Interface

Next, let's create the HTML file for our Quiz Application. This file will contain the essential elements for the application's structure, such as the header, timer, and placeholders for questions and choices. Note that the actual questions and choices will be dynamically generated and are not included directly in this file. Below is an example of the HTML code created for this tutorial:

index.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. <title

    >

    Quiz App with Timer</

    title

    >
  7. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200"

    /

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  9. <script

    src

    =

    "https://code.jquery.com/jquery-3.7.1.min.js"

    ></

    script

    >
  10. </

    head

    >
  11. <body

    >
  12. <div

    id

    =

    "main-wrapper"

    >
  13. <div

    id

    =

    "app-title"

    >

    Quiz App with Timer using HTML, CSS, and JavaScript</

    div

    >
  14. <div

    class

    =

    "flex-container align-center justify-center w-100"

    >
  15. <button

    id

    =

    "start-button"

    type

    =

    "button"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    contract_edit</

    span

    >

    Start Quiz</

    button

    >
  16. </

    div

    >

  17. <div

    id

    =

    "quiz-container"

    >
  18. <div

    class

    =

    "flex-container align-center justify-between w-100"

    >
  19. <div

    class

    =

    "flex-grow-1"

    >
  20. <span

    >

    Question #</

    span

    >
  21. <span

    id

    =

    "current_question_number"

    ></

    span

    >
  22. <span

    >

    &nbsp;

    out of&nbsp;

    </

    span

    >
  23. <span

    id

    =

    "total_question_number"

    ></

    span

    >
  24. </

    div

    >
  25. <div

    id

    =

    "timer-container"

    class

    =

    "flex-container align-center justify-center"

    >
  26. <span

    class

    =

    "material-symbols-outlined"

    >

    timer</

    span

    >
  27. <span

    id

    =

    "timer"

    >

    00:00</

    span

    >
  28. </

    div

    >
  29. </

    div

    >
  30. <hr

    class

    =

    "hr-light"

    >
  31. <div

    id

    =

    "quiz-list"

    ></

    div

    >
  32. <div

    id

    =

    "quiz-actions"

    class

    =

    "flex-container w-100 align-center justify-end"

    >
  33. <button

    id

    =

    "prev-question-btn"

    class

    =

    "quiz-action-btn"

    type

    =

    "button"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    chevron_left</

    span

    >

    Prev</

    button

    >
  34. <button

    id

    =

    "next-question-btn"

    class

    =

    "quiz-action-btn"

    type

    =

    "button"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    chevron_right</

    span

    >

    Next</

    button

    >
  35. <button

    id

    =

    "finish-question-btn"

    class

    =

    "quiz-action-btn"

    type

    =

    "button"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    check_small</

    span

    >

    Finish</

    button

    >
  36. </

    div

    >
  37. </

    div

    >

  38. <div

    id

    =

    "quiz-result"

    >
  39. <div

    id

    =

    "quiz-result-text"

    >
  40. </

    div

    >
  41. <div

    class

    =

    "flex-container align-center justify-center w-100"

    >
  42. <button

    id

    =

    "restart-button"

    type

    =

    "button"

    ><span

    class

    =

    "material-symbols-outlined"

    >

    contract_edit</

    span

    >

    Restart Quiz</

    button

    >
  43. </

    div

    >
  44. </

    div

    >
  45. </

    div

    >
  46. <script

    src

    =

    "script.js"

    ></

    script

    >
  47. </

    body

    >
  48. </

    html

    >

Step 3: Designing the Interface

Now, let's create the custom Cascading Stylesheet (CSS) for our application. This file defines the visual design and layout of the application's elements, ensuring a user-friendly and visually appealing interface. Below is the CSS code crafted for this tutorial:

style.css



  1. @import

    url

    (

    'https://fonts.googleapis.com/css2?f...00;0,700;1,300;1,400;1,500;1,700&display=swap'

    )

    ;

  2. *

    {
  3. font-family

    :

    "Ubuntu"

    ,

    serif

    ;
  4. font-weight

    :

    400

    ;
  5. font-style

    :

    normal

    ;
  6. box-sizing

    :

    border-box

    ;
  7. }

  8. html,

    body {
  9. margin

    :

    unset;
  10. padding

    :

    unset;
  11. height

    :

    100%

    ;
  12. min-height

    :

    100%

    ;
  13. width

    :

    100%

    ;
  14. min-width

    :

    100%

    ;
  15. overflow

    :

    auto

    ;
  16. }

  17. body {
  18. display

    :

    flex;
  19. flex-flow

    :

    column wrap;
  20. justify-content

    :

    center

    ;
  21. align-items

    :

    center

    ;
  22. background-color

    :

    #4290fb

    ;
  23. background-image

    :

    linear-gradient(

    89.5deg

    ,

    rgba

    (

    131

    ,

    204

    ,

    255

    ,

    1

    )

    0.4%

    ,

    rgba

    (

    66

    ,

    144

    ,

    251

    ,

    1

    )

    100.3%

    )

    ;
  24. }

  25. .fs-light

    {
  26. font-weight

    :

    300

    ;
  27. }

  28. .fs-midium

    {
  29. font-weight

    :

    500

    ;
  30. }

  31. .fs-bold

    {
  32. font-weight

    :

    700

    ;
  33. }

  34. #main-wrapper

    {
  35. max-height

    :

    100%

    ;
  36. width

    :

    100%

    ;
  37. max-width

    :

    500px

    ;
  38. padding

    :

    15px

    15px

    ;
  39. }

  40. #app-title

    {
  41. font-size

    :

    20px

    ;
  42. color

    :

    #fff

    ;
  43. font-weight

    :

    700

    ;
  44. letter-spacing

    :

    1px

    ;
  45. text-align

    :

    center

    ;
  46. margin-bottom

    :

    10px

    ;
  47. }

  48. .flex-container{
  49. display

    :

    flex;
  50. }

  51. .w-100

    {
  52. width

    :

    100%

    ;
  53. }

  54. .align-center{
  55. align-items

    :

    center

    ;
  56. }

  57. .justify-center{
  58. justify-content

    :

    center

    ;
  59. }

  60. .justify-between{
  61. justify-content

    :

    space-between;
  62. }

  63. .justify-end{
  64. justify-content

    :

    end

    ;
  65. }

  66. .flex-grow-1

    {
  67. flex-grow

    :

    1

    ;
  68. }

  69. hr.hr-light

    {
  70. border-color

    :

    #ffffff2b

    ;
  71. }


  72. #start-button

    ,
  73. #restart-button

    {
  74. display

    :

    flex;
  75. align-items

    :

    center

    ;
  76. justify-content

    :

    center

    ;
  77. background

    :

    #3D3BF3

    ;
  78. color

    :

    #fff

    ;
  79. border

    :

    unset;
  80. outline

    :

    unset;
  81. padding

    :

    5px

    30px

    ;
  82. border-radius

    :

    3px

    ;
  83. font-weight

    :

    600

    ;
  84. letter-spacing

    :

    1.2px

    ;
  85. cursor

    :

    pointer

    ;
  86. margin-bottom

    :

    20px

    ;
  87. }

  88. #start-button

    :

    hover

    ,
  89. #start-button

    :

    active

    ,
  90. #restart-button

    :

    hover

    ,
  91. #restart-button

    :

    active
  92. {
  93. background

    :

    #0200b6

    ;
  94. }

  95. #quiz-container

    {
  96. display

    :

    none

    ;
  97. width

    :

    100%

    ;
  98. padding

    :

    15px

    15px

    ;
  99. background

    :

    #fff

    ;
  100. border-radius

    :

    5px

    ;
  101. box-shadow

    :

    2px

    5px

    7px

    #00000068

    ;
  102. color

    :

    #494949

    ;
  103. }

  104. #timer

    {
  105. font-weight

    :

    600

    ;
  106. }

  107. div#quiz-list

    {
  108. width

    :

    100%

    ;
  109. padding

    :

    15px

    10px

    ;
  110. overflow

    :

    hidden

    ;
  111. /* position: relative; */
  112. display

    :

    flex;
  113. }

  114. #quiz-list

    .quiz-item{
  115. width

    :

    100%

    ;
  116. min-height

    :

    100%

    ;
  117. flex

    :

    none

    ;
  118. background-color

    :

    #fff

    ;
  119. }

  120. #quiz-list

    .trans-prev

    .quiz-item

    :

    not

    (

    .item-trans)

    {
  121. transform

    :

    translateX(

    -100%

    )

    ;
  122. }

  123. #quiz-list

    .trans-next

    .quiz-item

    .item-trans{
  124. transform

    :

    translateX(

    0%

    )

    ;
  125. }
  126. #quiz-list

    .trans-prev

    .quiz-item

    .item-trans{
  127. transform

    :

    translateX(

    -100%

    )

    ;
  128. }


  129. #quiz-list

    .quiz-item

    .animate-trans{
  130. transition

    :

    all .5s

    ease-in-out;
  131. }

  132. #quiz-list

    .trans-next

    .quiz-item

    .animate-trans

    .start

    .item-trans

    {
  133. transform

    :

    translateX(

    -100%

    )

    ;
  134. }

  135. #quiz-list

    .trans-prev

    .quiz-item

    .animate-trans

    .start

    .item-trans

    {
  136. transform

    :

    translateX(

    100%

    )

    ;
  137. }

  138. #quiz-list

    .trans-next

    .quiz-item

    .animate-trans

    .start

    :

    not

    (

    .item-trans)

    {
  139. transform

    :

    translateX(

    -100%

    )

    ;
  140. }

  141. #quiz-list

    .trans-prev

    .quiz-item

    .animate-trans

    .start

    :

    not

    (

    .item-trans)

    {
  142. transform

    :

    translateX(

    100%

    )

    ;
  143. }

  144. #quiz-list

    .quiz-item

    .quiz-item-question{
  145. font-size

    :

    14px

    ;
  146. padding

    :

    20px

    5px

    ;
  147. }

  148. #quiz-list

    .quiz-item

    .quiz-item-choices{
  149. width

    :

    100%

    ;
  150. }

  151. #quiz-list

    .quiz-item

    .quiz-choice{
  152. display

    :

    block

    ;
  153. width

    :

    100%

    ;
  154. padding

    :

    10px

    10px

    ;
  155. text-decoration

    :

    none

    ;
  156. color

    :

    inherit

    ;
  157. border

    :

    1px

    solid

    #49494929

    ;
  158. border-radius

    :

    3px

    ;
  159. margin-bottom

    :

    10px

    ;
  160. box-shadow

    :

    2px

    3px

    5px

    #0000002b

    ;
  161. transition

    :

    all .2s

    ease-in-out;
  162. }

  163. #quiz-list

    .quiz-item

    .quiz-choice

    :

    hover

    {
  164. transform

    :

    translateY(

    -2px

    )

    ;
  165. background

    :

    #f6f6f6

    ;
  166. }

  167. #quiz-list

    .quiz-item

    .quiz-choice

    .correct

    {
  168. transform

    :

    translateY(

    -2px

    )

    ;
  169. background

    :

    #52e4882b

    ;
  170. }

  171. #quiz-list

    .quiz-item

    .quiz-choice

    .wrong

    {
  172. transform

    :

    translateY(

    -2px

    )

    ;
  173. background

    :

    #e45c522b

    ;
  174. }

  175. #quiz-list

    .quiz-item

    .quiz-choice

    .disabled{
  176. user-select:

    none

    ;
  177. cursor

    :

    not-allowed;
  178. }

  179. button.quiz-action-btn

    {
  180. display

    :

    flex;
  181. align-items

    :

    center

    ;
  182. justify-content

    :

    center

    ;
  183. margin

    :

    0

    5px

    ;
  184. border

    :

    unset;
  185. border-radius

    :

    3px

    ;
  186. background

    :

    #0A5EB0

    ;
  187. color

    :

    #fff

    ;
  188. padding

    :

    5px

    15px

    ;
  189. cursor

    :

    pointer

    ;
  190. }
  191. #quiz-result

    {
  192. display

    :

    none

    ;
  193. }

  194. div#quiz-result-text

    {
  195. color

    :

    #fff

    ;
  196. text-align

    :

    center

    ;
  197. margin

    :

    20px

    0

    ;
  198. }

Step 4: Creating the Main Functions

Finally, let's create the JavaScript file. This file contains the essential JavaScript code that powers the functionality of the Quiz Application with Timer. The JavaScript manages features like loading questions, validating answers, keeping track of the timer, and displaying results. To make the code more efficient and easier to write, I have also utilized jQuery. Below is the JavaScript code for your reference:

script.js



  1. // Quiz Start Button
  2. const

    StartBtn =

    $(

    '#start-button'

    )
  3. // Quiz Restart Button
  4. const

    RestartBtn =

    $(

    '#restart-button'

    )
  5. // Quiz Questionnaire Container
  6. const

    Questionnaire =

    $(

    '#quiz-container'

    )
  7. // Quiz List Container
  8. const

    QuizList =

    $(

    "#quiz-list"

    )
  9. // Quiz Item Element to Clone
  10. const

    QuizItem =

    $(

    `
  11. <

    div class

    =

    "quiz-item"

    >
  12. <

    div class

    =

    "quiz-item-question"

    >

    Lorem ipsum dolor sit amet,

    consectetur adipiscing elit</

    div>
  13. <

    div class

    =

    "quiz-item-choices"

    >
  14. </

    div>
  15. </

    div>
  16. `)
  17. const

    QuizChoiceEL =

    $(

    `
  18. <

    a href=

    "javascript:void(0)"

    class

    =

    "quiz-choice"

    >

    Choice 101

    </

    a>
  19. `)
  20. // Previous Button
  21. const

    PrevBtn =

    $(

    '#prev-question-btn'

    )
  22. // Next Button
  23. const

    NextBtn =

    $(

    '#next-question-btn'

    )
  24. // Finish Button
  25. const

    FinishBtn =

    $(

    '#finish-question-btn'

    )
  26. // Current Question Number
  27. const

    CurrentQNumber =

    $(

    '#current_question_number'

    )
  28. // Total Question Number
  29. const

    TotalQNumber =

    $(

    '#total_question_number'

    )
  30. // Result Container
  31. const

    ResultContainer =

    $(

    '#quiz-result'

    )
  32. // Result Text
  33. const

    ResultText =

    $(

    '#quiz-result-text'

    )

  34. // Timer Element
  35. const

    timerEL =

    $(

    '#timer'

    )
  36. var

    questions =

    [

    ]

    ;
  37. var

    currentQindex =

    0

    ;
  38. var

    timerSec =

    150

    ;
  39. var

    score =

    0

    ;
  40. var

    answerArr =

    [

    ]

    ;

  41. var

    TimerInterval;

  42. // Load JSON Data
  43. async function

    fetch_data(

    )

    {
  44. return

    await fetch(

    "data.json"

    )
  45. .then

    (

    response =>

    {
  46. if

    (

    !

    response.ok

    )

    {
  47. console.error

    (

    response)

    ;
  48. }
  49. return

    response.json

    (

    )

    ;
  50. }

    )
  51. .then

    (

    data =>

    {
  52. return

    (

    data)
  53. }

    )
  54. .catch

    (

    error =>

    {
  55. console.log

    (

    error)
  56. }

    )
  57. }

  58. function

    shuffle(

    arrData)

    {
  59. let curIndx =

    arrData.length

    ;
  60. while (

    curIndx !=

    0

    )

    {
  61. let randIndx =

    Math

    .floor

    (

    Math

    .random

    (

    )

    *

    curIndx)

    ;
  62. curIndx--;

  63. [

    arrData[

    curIndx]

    ,

    arrData[

    randIndx]

    ]

    =

    [
  64. arrData[

    randIndx]

    ,

    arrData[

    curIndx]

    ]

    ;
  65. }
  66. }

  67. function

    check_ans(

    el)

    {
  68. el.find

    (

    ".quiz-choice"

    )

    .click

    (

    function

    (

    e)

    {
  69. e.preventDefault

    (

    )
  70. if

    (

    $(

    this

    )

    .hasClass

    (

    "disabled"

    )

    )
  71. return

    ;
  72. var

    choiceKey =

    $(

    this

    )

    [

    0

    ]

    .dataset

    .key
  73. var

    id =

    el[

    0

    ]

    .dataset

    .id
  74. var

    correct_ans;

  75. for

    (

    var

    i =

    0

    ;

    i <

    questions.length

    ;

    i++

    )

    {
  76. if

    (

    questions[

    i]

    .id

    ==

    id)

    {
  77. correct_ans =

    questions[

    i]

    .answer
  78. }
  79. }
  80. if

    (

    choiceKey ==

    correct_ans)

    {
  81. $(

    this

    )

    .addClass

    (

    "correct"

    )

    ;
  82. score++;
  83. }

    else

    {
  84. $(

    this

    )

    .addClass

    (

    "wrong"

    )
  85. el.find

    (

    `.quiz

    -

    choice[

    data-

    key=

    "${correct_ans}"

    ]

    `)

    .addClass

    (

    "correct"

    )
  86. }
  87. el.find

    (

    `.quiz

    -

    choice`)

    .addClass

    (

    "disabled"

    )
  88. answerArr.push

    (

    {

    id:

    id,

    correct:

    correct_ans,

    wrong:

    (

    (

    choiceKey !=

    correct_ans)

    ?

    choiceKey :

    ""

    )

    }

    )
  89. }

    )
  90. }

  91. function

    showResult(

    )

    {
  92. Questionnaire.toggle

    (

    false

    )
  93. ResultText.text

    (

    `Your Total Correct Answer is ${

    score}

    out of `+

    questions.length

    +

    `.`)
  94. ResultContainer.toggle

    (

    true

    )
  95. clearInterval(

    TimerInterval)
  96. }

  97. // Check if Question Already Answered
  98. function

    chech_if_answered(

    el)

    {
  99. var

    id =

    el[

    0

    ]

    .dataset

    .id

    ;
  100. if

    (

    answerArr.length

    >

    0

    )

    {
  101. for

    (

    var

    i =

    0

    ;

    i <

    answerArr.length

    ;

    i++

    )

    {
  102. if

    (

    answerArr[

    i]

    .id

    ==

    id)

    {
  103. el.find

    (

    `.quiz

    -

    choice[

    data-

    key=

    "${answerArr.correct}"

    ]

    `)

    .addClass

    (

    "correct"

    )

    [*]if

    (

    answerArr[

    i]

    .wrong

    >

    0

    )

    [*]el.find

    (

    `.quiz

    -

    choice[

    data-

    key=

    "${answerArr.wrong}"

    ]

    `)

    .addClass

    (

    "wrong"

    )

    ;

    [*]el.find

    (

    `.quiz

    -

    choice`)

    .addClass

    (

    "disabled"

    )

    [*]}

    [*]}

    [*]}

    [*]}

    [*]

    [*]// Computing and Updating Timer

    [*]function

    computeTimer(

    time)

    {

    [*]var

    min =

    Math

    .floor

    (

    time /

    60

    )

    |

    0

    ;

    [*]var

    sec =

    Math

    .ceil

    (

    time -

    (

    min *

    60

    )

    )

    ;

    [*]

    [*]min =

    String

    (

    min)

    .padStart

    (

    2

    ,

    '0'

    )

    ;

    [*]sec =

    String

    (

    sec)

    .padStart

    (

    2

    ,

    '0'

    )

    ;

    [*]

    [*]timerEL.text

    (

    `${

    min}

    :

    ${

    sec}

    `)

    [*]}

    [*]function

    startQuiz(

    )

    {

    [*]if

    (

    !

    questions[

    currentQindex]

    )

    {

    [*]alert(

    "Questions are not listed yet!"

    )

    ;

    [*]return

    ;

    [*]}

    [*]currentQindex =

    0

    ;

    [*]score =

    0

    ;

    [*]score =

    0

    ;

    [*]answerArr =

    [

    ]

    ;

    [*]QuizList.html

    (

    ''

    )

    [*]CurrentQNumber.text

    (

    currentQindex+

    1

    )

    [*]

    [*]timerSec =

    150

    ;

    [*]computeTimer(

    timerSec)

    [*]StartBtn.toggle

    (

    false

    )

    [*]

    [*]Questionnaire.toggle

    (

    true

    )

    [*]

    [*]PrevBtn.toggle

    (

    false

    )

    [*]NextBtn.toggle

    (

    false

    )

    [*]FinishBtn.toggle

    (

    false

    )

    [*]

    [*]var

    itemEL =

    QuizItem.clone

    (

    true

    )

    ;

    [*]itemEL[

    0

    ]

    .dataset

    .id

    =

    questions[

    currentQindex]

    .id

    ;

    [*]itemEL.find

    (

    ".quiz-item-question"

    )

    .text

    (

    questions[

    currentQindex]

    .question

    )

    ;

    [*]

    [*]for

    (

    var

    i =

    0

    ;

    i <

    questions[

    currentQindex]

    .choices

    .length

    ;

    i++

    )

    {

    [*]var

    choiceEl =

    QuizChoiceEL.clone

    (

    true

    )

    ;

    [*]choiceEl[

    0

    ]

    .dataset

    .key

    =

    i;

    [*]choiceEl.text

    (

    questions[

    currentQindex]

    .choices

    [

    i]

    )

    ;

    [*]itemEL.find

    (

    ".quiz-item-choices"

    )

    .append

    (

    choiceEl)

    [*]}

    [*]QuizList.append

    (

    itemEL)

    ;

    [*]check_ans(

    itemEL)

    ;

    [*]NextBtn.toggle

    (

    true

    )

    ;

    [*]TimerInterval =

    setInterval(

    function

    (

    )

    {

    [*]timerSec--;

    [*]computeTimer(

    timerSec)

    [*]console.log

    (

    timerSec)

    [*]if

    (

    timerSec <=

    0

    )

    {

    [*]clearInterval(

    TimerInterval)

    ;

    [*]showResult(

    )

    ;

    [*]return

    ;

    [*]}

    [*]}

    ,

    1000

    )

    [*]}

    [*]$(

    document)

    .ready

    (

    function

    (

    )

    {

    [*]// Load Data

    [*]fetch_data(

    )

    .then

    (

    questionData =>

    {

    [*]questions =

    questionData;

    [*]shuffle(

    questions)

    ;

    [*]TotalQNumber.text

    (

    questions.length

    )

    [*]

    [*]// Starting the Quiz event

    [*]StartBtn.click

    (

    function

    (

    e)

    {

    [*]e.preventDefault

    (

    )

    [*]startQuiz(

    )

    [*]}

    )

    [*]

    [*]// Navigate to Next Question Event

    [*]NextBtn.click

    (

    function

    (

    e)

    {

    [*]e.preventDefault

    (

    )

    [*]currentQindex++;

    [*]CurrentQNumber.text

    (

    currentQindex+

    1

    )

    [*]

    [*]PrevBtn.attr

    (

    "disabled"

    ,

    true

    )

    [*]NextBtn.attr

    (

    "disabled"

    ,

    true

    )

    [*]FinishBtn.attr

    (

    "disabled"

    ,

    true

    )

    [*]

    [*]var

    itemEL =

    QuizItem.clone

    (

    true

    )

    ;

    [*]

    [*]itemEL[

    0

    ]

    .dataset

    .id

    =

    questions[

    currentQindex]

    .id

    ;

    [*]itemEL.find

    (

    ".quiz-item-question"

    )

    .text

    (

    questions[

    currentQindex]

    .question

    )

    ;

    [*]

    [*]for

    (

    var

    i =

    0

    ;

    i <

    questions[

    currentQindex]

    .choices

    .length

    ;

    i++

    )

    {

    [*]var

    choiceEl =

    QuizChoiceEL.clone

    (

    true

    )

    ;

    [*]choiceEl[

    0

    ]

    .dataset

    .key

    =

    i;

    [*]choiceEl.text

    (

    questions[

    currentQindex]

    .choices

    [

    i]

    )

    ;

    [*]itemEL.find

    (

    ".quiz-item-choices"

    )

    .append

    (

    choiceEl)

    [*]}

    [*]itemEL.addClass

    (

    "item-trans"

    )

    [*]QuizList.addClass

    (

    "trans-next"

    )

    [*]QuizList.append

    (

    itemEL)

    ;

    [*]check_ans(

    itemEL)

    ;

    [*]chech_if_answered(

    itemEL)

    ;

    [*]// itemEL.css("width", Questionnaire.innerWidth())

    [*]// itemEL.siblings(".quiz-item").css("width", Questionnaire.innerWidth())

    [*]

    [*]itemEL.addClass

    (

    "animate-trans"

    )

    [*]itemEL.siblings

    (

    ".quiz-item"

    )

    .addClass

    (

    "animate-trans"

    )

    [*]setTimeout(

    function

    (

    )

    {

    [*]

    [*]itemEL.addClass

    (

    "start"

    )

    [*]itemEL.siblings

    (

    ".quiz-item"

    )

    .addClass

    (

    "start"

    )

    [*]}

    ,

    100

    )

    [*]setTimeout(

    function

    (

    )

    {

    [*]QuizList.removeClass

    (

    "trans-next"

    )

    [*]

    [*]itemEL.removeClass

    (

    "item-trans"

    )

    [*]itemEL.removeClass

    (

    "animate-trans"

    )

    [*]itemEL.removeClass

    (

    "start"

    )

    [*]

    [*]itemEL.siblings

    (

    ".quiz-item"

    )

    .remove

    (

    )

    [*]

    [*]PrevBtn.attr

    (

    "disabled"

    ,

    false

    )

    [*]NextBtn.attr

    (

    "disabled"

    ,

    false

    )

    [*]FinishBtn.attr

    (

    "disabled"

    ,

    false

    )

    [*]}

    ,

    500

    )

    [*]PrevBtn.toggle

    (

    currentQindex >

    0

    )

    [*]NextBtn.toggle

    (

    currentQindex <

    (

    questions.length

    -

    1

    )

    )

    [*]FinishBtn.toggle

    (

    currentQindex ==

    (

    questions.length

    -

    1

    )

    )

    [*]}

    )

    [*]

    [*]// Navigate to Prev Question Event

    [*]PrevBtn.click

    (

    function

    (

    e)

    {

    [*]e.preventDefault

    (

    )

    [*]currentQindex--;

    [*]CurrentQNumber.text

    (

    currentQindex+

    1

    )

    [*]

    [*]PrevBtn.attr

    (

    "disabled"

    ,

    true

    )

    [*]NextBtn.attr

    (

    "disabled"

    ,

    true

    )

    [*]FinishBtn.attr

    (

    "disabled"

    ,

    true

    )

    [*]

    [*]var

    itemEL =

    QuizItem.clone

    (

    true

    )

    ;

    [*]

    [*]itemEL[

    0

    ]

    .dataset

    .id

    =

    questions[

    currentQindex]

    .id

    ;

    [*]itemEL.find

    (

    ".quiz-item-question"

    )

    .text

    (

    questions[

    currentQindex]

    .question

    )

    ;

    [*]

    [*]for

    (

    var

    i =

    0

    ;

    i <

    questions[

    currentQindex]

    .choices

    .length

    ;

    i++

    )

    {

    [*]var

    choiceEl =

    QuizChoiceEL.clone

    (

    true

    )

    ;

    [*]choiceEl[

    0

    ]

    .dataset

    .key

    =

    i;

    [*]choiceEl.text

    (

    questions[

    currentQindex]

    .choices

    [

    i]

    )

    ;

    [*]itemEL.find

    (

    ".quiz-item-choices"

    )

    .append

    (

    choiceEl)

    [*]}

    [*]itemEL.addClass

    (

    "item-trans"

    )

    [*]QuizList.addClass

    (

    "trans-prev"

    )

    [*]QuizList.prepend

    (

    itemEL)

    ;

    [*]check_ans(

    itemEL)

    ;

    [*]chech_if_answered(

    itemEL)

    ;

    [*]

    [*]// itemEL.css("width", Questionnaire.innerWidth())

    [*]// itemEL.siblings(".quiz-item").css("width", Questionnaire.innerWidth())

    [*]

    [*]

    [*]setTimeout(

    function

    (

    )

    {

    [*]itemEL.addClass

    (

    "start"

    )

    [*]itemEL.siblings

    (

    ".quiz-item"

    )

    .addClass

    (

    "start"

    )

    [*]itemEL.addClass

    (

    "animate-trans"

    )

    [*]itemEL.siblings

    (

    ".quiz-item"

    )

    .addClass

    (

    "animate-trans"

    )

    [*]}

    ,

    100

    )

    [*]setTimeout(

    function

    (

    )

    {

    [*]QuizList.removeClass

    (

    "trans-prev"

    )

    [*]

    [*]itemEL.removeClass

    (

    "item-trans"

    )

    [*]itemEL.removeClass

    (

    "animate-trans"

    )

    [*]itemEL.removeClass

    (

    "start"

    )

    [*]

    [*]itemEL.siblings

    (

    ".quiz-item"

    )

    .remove

    (

    )

    [*]

    [*]PrevBtn.attr

    (

    "disabled"

    ,

    false

    )

    [*]NextBtn.attr

    (

    "disabled"

    ,

    false

    )

    [*]FinishBtn.attr

    (

    "disabled"

    ,

    false

    )

    [*]}

    ,

    500

    )

    [*]PrevBtn.toggle

    (

    currentQindex !=

    0

    )

    [*]NextBtn.toggle

    (

    currentQindex <

    (

    questions.length

    -

    1

    )

    )

    [*]FinishBtn.toggle

    (

    false

    )

    [*]}

    )

    [*]FinishBtn.click

    (

    function

    (

    e)

    {

    [*]e.preventDefault

    (

    )

    [*]showResult(

    )

    [*]}

    )

    [*]

    [*]RestartBtn.click

    (

    function

    (

    e)

    {

    [*]e.preventDefault

    (

    )

    [*]ResultContainer.toggle

    (

    false

    )

    [*]startQuiz(

    )

    [*]}

    )

    [*]

    [*]}

    )

    [*]}

    )



Snapshotes

Below are some snapshots showcasing the results of the Quiz Application with Timer created using the source code provided above. These images demonstrate the application's interface and functionality, including the dynamic quiz questions, timer, and result display:

When Application Page is Loaded

hs-quiz-app-page-loaded.PNG


Question and Answer Validation

hs-quiz-app-page-questionnaire.PNG


Result

hs-quiz-app-result.PNG


I have provided the complete source code as a downloadable zip file on this website. You can find the download button located at the end of this article. Feel free to download the source code and customize it to suit your specific project requirements or to explore and enhance its functionality.

There you have it. I hope this Quiz Application with Timer Tutorial will help you with what you are loking for and you'll find something useful for your future web application.

Explore more on this website for more Tutorials, Free Source Codes, and Articles covering various programming languages.

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

449,093

322,160

322,169

Top