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

Share this video with your friends

Social Share Links

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.

Kevin Cunningham: [0:00] We would like a CLI tool that asks us a question, evaluates the answer, and then logs the results. We've handled the first two. We are asking the question from the file system. We're evaluating the results. In this case, we're just checking whether or not the question and the answer match. We haven't handled logging the results. [0:17] Before we do that, let's think about the data structure we want to persist. Our data structure for each of the questions that we're persisting to our JSON file, we'll have an id, which will be an integer. We'll have a question, which is going to be a string.

[0:31] We'll have an answer, which will also be a string. We'll have a created, which will be a date string, and a lastAsked will be a date string. We'll have a lastAnsweredCorrect, will be a Boolean, so was it last answered correct?

[0:45] At the moment, we're reading our file, we're asking a particular question, and then we're checking the answer. Let's refactor this slightly and have our target be our parsedData and destructure from our target.

[1:07] We're doing that because when there's many questions in here and when we're selecting at random, we want to be able to have the specific question that we're dealing with assigned to a variable. We're then going to ask the question and check the answer. This checkAnswer, as well as logging, let's have it return true if it's right and false if it's wrong.

[1:30] We're going to set the target.lastAnsweredCorrect to be the return value of our checkAnswer. Let's set target.lastAsked to date().toString(). Now, we want to persist it to the file system.

[1:50] We'll need to create a newData array, which is the old data array without the current target. Then, we want to push the newData array, our target. Then, we want to write. We'll await .writeFile. This works very similarly to readFile. We pass in a path and then what we want to write. In this case, we want the stringified version of our newData.

[2:20] Let's try this out. If we check our data, we can see lastAnswered was set to true. We now have a lastAsked. We do it again. This time, pass in the wrong answer. We'll see our lastAnsweredCorrect is updated to false.

[2:35] Now, we're reading from a file, we're getting the question, we're asking the question, we're checking the answer, and we're writing to the file.