Recommended way to store values for this circumstance
Hey, let's say you have an example on an educational site like the codepen example. You have a set of questions and it tells you if your answers are right or wrong. What would be a recommended way to store the correct answers for it to test against as i know that storing them in an
if
statement isn't optimal. Thinking on a larger scale, let's say the site has a large number of these question boxes, it'd have to be something that could cater for that many values and Ideally something users can't f12
their way to like storing it in a data attribute.
I'm trying to learn best practices for these sorts of things. Thanks in advance.
https://codepen.io/deerCabin/pen/dyBGzpX7 Replies
You can use an array of correct answers. If they are in the same order as the questions, you can use the question number as the index for the array to access the correct answer for that question.
Ah thatβs quite a good method, thank you
or just use an array of objects
where each object holds question + ans
then iterate through the array, and for each question create html elements to show question and input for answer. and set the index as an attribute to those elements
so if u use forEach() then the 2nd param is the index.
this way when u need to ceck the answer fot a specific question, u can get the index of it from the attribute and use that to index into the array
i'll take a look into it, thanks. I'm quite new to js, is an object the equivelant to a dictionary in other languages or are there dictionaries in js too?
m not a py dev
but from the little so i have have used it felt quite the same
ah okay, i appreciate the info
welcm