Log Question Results by using Node.js to Filter and Write to a File

Share this video with your friends

Send Tweet

Now that we have the basic question/answer functionality working, that being we can ask a question of the user and check if the users answer is correct, we want to log the results of the users answers back to the file.

To log the results, we'll assign the question we got to from our file to a variable and add lastAnsweredCorrect (that is true if the user answers the question correctly and false if not) and lastAsked (the current date) properties to that object. We will then add that question object back to the array of questions we got from the file system and use readFile to write our results.

Note: checkAnswer was refactored into its own function for easier visibility in our script.