This Node.js application runs a timed chemistry quiz based on a set of predefined questions stored in a JSON file.
- Randomly selects 10 questions from a pool of chemistry questions generated from the chemistry Gaia node
- 10-second timer for each question
- 5 points awarded for correct answers, 0 for incorrect answers
- No ability to return to previous questions
- Displays total score at the end of the quiz
- Node.js installed on your system
quiz_data.json
file in the same directory as the script
- Save the provided JavaScript code in a file named
index.js
. - Ensure
quiz_data.json
is in the same directory asindex.js
.
Run the following command in your terminal:
node index.js
The application is structured as follows:
-
Importing Required Modules:
readline
for handling user inputfs
for reading the JSON file
-
Data Loading and Preparation:
- Reads and parses the
quiz_data.json
file - Shuffles the questions randomly using the
shuffleArray
function
- Reads and parses the
-
Quiz Logic:
askQuestion()
: Main function that handles asking questions, timing, and scoringendQuiz()
: Displays the final score and closes the readline interface
-
User Interaction:
- Uses readline to get user input for answers
- Provides immediate feedback on correct/incorrect answers
-
Timing Mechanism:
- Uses
setInterval
to create a 10-second countdown for each question
- Uses
shuffleArray(array)
: Randomizes the order of elements in an arrayaskQuestion()
: Manages the flow of the quiz, including displaying questions, handling user input, and timingendQuiz()
: Concludes the quiz and displays the final score
- The application starts by welcoming the user and explaining the rules.
- For each question:
- The question and options are displayed.
- A 10-second timer starts.
- The user can input their answer (1-4).
- If the timer expires before an answer is given, the quiz moves to the next question.
- Immediate feedback is provided for each answer.
- After all questions are answered or timed out, the total score is displayed.
To modify the quiz questions or add more, edit the quiz_data.json
file. Ensure each question object has:
- A
question
field (string) - An
options
field (array of strings) - A
correctAnswer
field (string matching one of the options)
- Console-based interface
- No persistence of scores between runs
- Fixed number of questions and time per question