# Checking answer to a question in array

In an array of elements, say 20, every fifth element is a question, and one of the elements is the answer to that question. It’s like a quiz. How can I check if the user has selected the right answer? Any help would be appreciated.

Why don’t you show us what you think your algorithm would be to solve such a problem?

Based on what you state above, I understand you have twenty elements and every fifth element is a question, but when you state “one of the elements is the answer to that question” we need some more information to assist you.

• Can you clarify which elements (indices) for any particular other fifth element (question) could possibly contain the answer to that particular question?
• Without knowing what the actual answer is for a particular question, there is no way for me to tell you which element contains the answer element.

I have a feeling there is more to this problem than you have stated.

Alos, You already have a topic related to a slide show quiz. If this is related to that, please ask your question there instead of creating a new topic.

It’s a totally different one.
Let me clarify. The questions are on the indices 4, 8, 12 & so on. The answer to the first question is between indices 0 & 8, and the answer to the second question is between indices 9 & 17, and so on. It means out of first 9 elements one is question whose index is fixed; and other elements are options out of which one is answer whose index is not fixed.

Please let me know if I am not clear or if you want further explanation.

This new information does help explain more, but I must be missing what you are wanting the application to do with this information. So question #1 is located at index 4. The answer to the question is located in either index 0, 1, 2, 3, 5, 6, 7, 8. What exactly are you wanting to do with this information? It might be better to show an example of the first 9 elements in your array and then explain exactly what you want to be able to do with this data.

I am taking a guess here, but if you had a separate array just containing the indices with the correct answer for each of the questions in the main array, then you should be able to use that to validate which option (I assume is represented by an input of type=“radio”) by the index of the option of the same name attribute value.

There are other ways to approach this which would involve giving each input (option) a unique id and store the id of the correct answer for each question as the value of an object property represented by the question order index. For example:

``````const correcAnswers = {
0: '8612dkjd62',
1: '362sdlsd3l',
2: 'sdlgjsd232',
3: '26432sdg36'
}
``````

The above would be the first 4 questions and the the id of the input with the correct answer.

1 Like

Thanks a lot again. I have got the point. I am obliged.