Hi Everone, welcome to my blog and in this blog post i will share the code of JavaScript practice project "QUIZ" for beginners along with explaination. You can copy the code by clicking the button just above the code. Read the explaiantion for better understanding.
let's get started with index.html file:
index.html
style.css:
index.js:
Here is the explaination:
The questions array stores objects, each representing a question with its choices and the index of the correct answer.
Event listener: When the DOM content is loaded, the displayCurrentQuestion function is called to show the first question.
The user interface elements are accessed using the querySelector method to display the current question, handle user input, and show quiz messages.
When the user clicks the "Next Question" button, an event listener triggers the next action. If the quiz is not over:
It checks if the user has selected an answer. If not, it displays a message asking the user to select an answer.
If an answer is selected, it checks if the selected answer index matches the correct answer index for the current question. If it matches, it increments the correctAnswers count.
It increments the currentQuestion index to move to the next question.
If there are more questions, it calls displayCurrentQuestion to show the next question. Otherwise, it calls displayScore to show the final score and changes the button text to "Play Again?".
If the quiz is over and the user clicks "Next Question":
It resets the quiz by setting currentQuestion and correctAnswers to zero.
It hides the score display.
It calls displayCurrentQuestion to show the first question again.
The displayCurrentQuestion function updates the question and choice elements based on the current question index:
It gets the question, choice list, and number of choices for the current question.
It updates the question text on the UI.
It clears the choice list.
It creates new list items (li) for each choice and appends them to the choice list.
The resetQuiz function resets the quiz by setting the current question and correct answers to zero.
The displayScore function shows the user's score by updating the result text and displaying it on the UI.
The hideScore function hides the score display by setting its CSS display property to "none".
FINAL WORDS:
Thank you Don't forget to leave your feedback in the comment section. Follow front.end24 on instagram for more project ideas
index.html
style.css:
index.js:
Here is the explaination:
The questions array stores objects, each representing a question with its choices and the index of the correct answer.
Event listener: When the DOM content is loaded, the displayCurrentQuestion function is called to show the first question.
The user interface elements are accessed using the querySelector method to display the current question, handle user input, and show quiz messages.
When the user clicks the "Next Question" button, an event listener triggers the next action. If the quiz is not over:
It checks if the user has selected an answer. If not, it displays a message asking the user to select an answer.
If an answer is selected, it checks if the selected answer index matches the correct answer index for the current question. If it matches, it increments the correctAnswers count.
It increments the currentQuestion index to move to the next question.
If there are more questions, it calls displayCurrentQuestion to show the next question. Otherwise, it calls displayScore to show the final score and changes the button text to "Play Again?".
If the quiz is over and the user clicks "Next Question":
It resets the quiz by setting currentQuestion and correctAnswers to zero.
It hides the score display.
It calls displayCurrentQuestion to show the first question again.
The displayCurrentQuestion function updates the question and choice elements based on the current question index:
It gets the question, choice list, and number of choices for the current question.
It updates the question text on the UI.
It clears the choice list.
It creates new list items (li) for each choice and appends them to the choice list.
The resetQuiz function resets the quiz by setting the current question and correct answers to zero.
The displayScore function shows the user's score by updating the result text and displaying it on the UI.
The hideScore function hides the score display by setting its CSS display property to "none".
FINAL WORDS:
Thank you Don't forget to leave your feedback in the comment section. Follow front.end24 on instagram for more project ideas

Comments
Post a Comment