Skip to content Skip to sidebar Skip to footer

Matching Elements From Two Arrays In Javascript

I am trying to make a small random quizz generator, first array has the questions and they are randomly displayed (when page is loaded or when page is refreshed), the second array

Solution 1:

Right now you have two lists. One contains questions, the other answers:

var questions = [ "2 + 2", "2 + 1", "1 + 1" ];
var answers = [ "4", "3", "2" ];

In order to easily check your user input, it's a good idea to convert this data structure to an object in which the question is the key, and the answer the value.

Note that this requires your question and answer combinations to be unique. I.e.: a question can not have multiple answers

var questionsWithAnswers = {
  "2 + 2": "4",
  // etc.
}

You can convert the data in javascript, so you don't have to rewrite the lists:

var questionsWithAnswers = {};
for (let i = 0; i < questions.length; i += 1) {
  questionsWithAnswers[question[i]] = answers[i];
}

Now, your check for a question is:

var answerIsCorrect = questionsWithAnswers[question] === userInput;

Solution 2:

First of all, you do understand that having a question and answer available in JS is not a good idea, cause user can see the code.

Anyway, I would combine questions and answer into one structure.

var QAs = [["How many sides has a hexagon", "6"],
    ["A circle has...degrees?", "360"]]; // and so onlet randomItem = QAs[Math.floor(Math.random()*myQs.length)];

document.getElementById("demo").innerHTML = randomItem[0];

functionuserAnswer() {
    const check = document.getElementById('answer').value;
    if (check == randomItem[1]) {  // check randomItem availability in this scope. otherwise save to to window.randowItem scope.
        sayMessage = check + "  is the correct answer!";
    } else {
        sayMessage = check + "  is not the correct answer....";
    }
    document.getElementById("userA").innerHTML = sayMessage;
}

Solution 3:

I'd recommend revising the schema of your questions.

Housing questions and answers together within objects would probably be the least error prone approach.

See below for a practical example.

// Questions.const questions = [
  {q: 'How may sidea has a hexagon', a: 6},
  {q: 'A circle has...degrees?', a: 360},
  {q: '2^3=...?', a: 8},
  {q: '2+2:2=..?', a: 3},
  {q: 'A triangle has....degrees?', a: 180},
  {q: 'Square root of 2 is...?', a: 1.41}
]

// Selected.const selected = questions[Math.floor(Math.random()*questions.length)]

// Question.const question = selected.qdocument.getElementById('question').innerHTML = question

// Normalise.constnormalise = (number) => Math.round(parseFloat(number), 2)

// Submit Answer.document.getElementById('form').addEventListener('submit', (event) => {
  event.preventDefault()
  const userAnswer = normalise(document.getElementById('answer').value)
  const actualAnswer = normalise(selected.a)
  const isCorrect = (userAnswer == actualAnswer)
  document.getElementById('feedback').innerHTML = isCorrect && 'Correct 👍' || 'Incorrect 👎'
})
<div><pid="question"></p></div><formid="form"><div><inputid="answer"placeholder="Answer .."/></div><button>Submit answer</button></form><div><pid="feedback"></p><div>

Post a Comment for "Matching Elements From Two Arrays In Javascript"