How to match a users test answers to correct answers

Hello freeCodeCamp Community,

I am trying to create an app that allows a user to take multiple choice quiz. I am stuck at how to match the users chosen answers with the correct answers in MongoDB. Below is the relevant part of my code so far. Please help. Thank you.

       app.route('/submit') 
          .post((req, res) => {
            let userAnswers = req.body;
            console.log(userAnswers);
            //pull database of answers and check them
            Ques.find({}, (err, doc)=>{
                console.log(doc);
            })
            res.redirect('/');
        })``

The result of console.log(userAnswers) is below:
``
{ 'ans-0': 'The Terminator',
  'ans-1': 'SitePoint',
  'ans-2': 'Sitting in a tree' }

The result of console.log(doc) is below:

[{ _id: 5be0d64b15018a580407660b,
  question: 'Who is the strongest?',
  answers: { a: 'Superman', b: 'The Terminator', c: 'Waluigi, obviously' },
  correctAnswer: 'c' }
{ 'ans-0': 'Waluigi, obviously',
  'ans-1': 'Simple Steps Code',
  'ans-2': 'Exploring the Pacific Ocean' }
{ _id: 5be0d64b15018a580407660b,
  question: 'Who is the strongest?',
  answers: { a: 'Superman', b: 'The Terminator', c: 'Waluigi, obviously' },
  correctAnswer: 'c' }]

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like