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/dyBGzpX
7 Replies
Chooβ™šπ•‚π•šπ•Ÿπ•˜
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.
snxxwyy
snxxwyyβ€’2mo ago
Ah that’s quite a good method, thank you
glutonium
glutoniumβ€’2mo ago
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
snxxwyy
snxxwyyβ€’2mo ago
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?
glutonium
glutoniumβ€’2mo ago
m not a py dev but from the little so i have have used it felt quite the same
snxxwyy
snxxwyyβ€’2mo ago
ah okay, i appreciate the info
glutonium
glutoniumβ€’2mo ago
welcm
Want results from more Discord servers?
Add your server