Offload Complex Logic and Confirm a Correct Answer with the User

Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated a year ago

One issue with our script is that the answer that is checked needs to be exactly the same string as the answer in the file system. This can be brittle because your user might not capitalize or forget a space. There are ways we could sanitize user data but this can introduce quite a lot of complex logic into our script.

A great work around is to use Inquirer to check with the user if they got the answer right or wrong, this way you know you are logging correct values and you don't need to worry about sanitizing data as the user is determining for themself if they were right.

Instructor: [0:00] At the moment, the input and the answer have to match each other exactly. That means if I give a text response instead of a numerical response for that answer, if I forget to uppercase Vermeer's first letter in his name, I'm always going to be marked wrong. [0:17] There are lots of different ways that we can handle this. One approach would be to sanitize the input and the answer in the same way and compare them. Maybe we want to get rid of all the spaces and we wanted to make it all lowercase and want to apply these changes to both the input and the answer.

[0:36] Now, when I run it, which country has more permits. I can spell Sudan with a lowercase s and get it correct. For more complex descriptive answers, this starts to fall down whether or not I use commas or semicolons in my setting of the answer. It's going to matter massively if I want to make sure I get the right answer.

[0:56] Another approach is to allow the user to make the decision on whether or not they were right or wrong. So let's Implement that. What we'll do is we'll log out and we'll say, "You answered," input the actual answer, and then we're going to use inquirer. Response equals a weirdinquirer.com.

[1:17] We're going to say, "Ask the user, did you get it right?" Now, the type this time that we're going to have is a confirm. We're going to give it a meme of jack. So let's console.log response. Now this is processed on exit one, here, at that point.

[1:36] Let's run it. According to Greek mythology, what was Pandora? You answer Pandora? The actual answer is Pandora. Did you get it right? You can see, because it's confirm, the "Y" is upper case, which means it's the default answer. I just press return, I'm going to get that.

[1:52] I'm going to get a Boolean set to 1. I do it again. "Margaret Thatcher." Maybe I misspell it, or add a comma.Did I get it wrong? No, so we're getting a Boolean? OK.

[2:04] At that case, then, all we need to do is to return our response.jack. Let's try it out. 22 is the answer. Did I get it right? Yes. Now, if I check my bingo game, check my last saying, that last answer correct is set to true.

[2:19] According to Greek mythology, who's the first woman? It's still Pandora. Did you get it? I can say, no, this time and Pandora, last answer is false.

[2:28] In this way, we're offloading the checking logic to the actual user. We're allowing them to decide whether or not their answer is right or wrong. Since the user is trying to improve their knowledge, they're motivated to give their correct answer here.

egghead
egghead
~ 16 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today