I have series of variables that have similar identifiers. These variable identifiers increase by 1. For example:
var q1 = "5";
var q2 = "12";
var q3 = "98";
.
.
var q10 = "1001";
On my page I have a textarea (id="testbox") for user input which is defined as:
var res = document.getElementById("testbox").value;
Essentially, the textarea has multiple correct values. These are all the values which were assigned to var q1...var q10. I wrote the following code (see below), which works just fine, but I am wondering if there is a more efficient way.
if ( res == q1 || res == q2 || res == q3 || res == q4 || res == q5 || res == q6 || res == q7 || res == q8 || res == q9 || res == q10 )
{
//something good happens
}
else {
//something bad happens
}
Is there a way to create iterations of my variable identifier and check that to my user input. Is there some way to increase a particular value of my identifier by 1 and stop at 10 (var q1...var q10) and test that condition to the variable "res".
You can make an array with the values instead of storing them in variables and then you can iterate over the array to see if res matches any of the entries. Let arr = ["5",..."1001"];