javascript if within for loop creating compare field while looping

I am trying to create this code in javascript.  The end result needs to be able to run not on a server.

 for (i = 0; i < 20; i++)   //for loop looping through an array
    {  
       if (clientText == null )  //verifying the entry exists in the table -- I was not able to post with the proper syntax for the array
       {
       }
       else
       {
      //   here I am doing stuff with the array values. this code is not an issue.
             
          var holdType = 'Reason' + i;  //I have fields in the javascript Reason1, Reason2, Reason3, etc.  I want to create the name of the field on the fly to determine if the field contains a particular value.


           if (holdType == 1)   // comparing Reason1 = 1; Reason2=1; etc; this would allow me to build the entire code in a loop.
           {
             htm += 'X';
           }
                }
    }

Open in new window

LVL 1
kw66722Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sar1973Commented:
You could use the eval function:
if (eval("Reason"+i+"==(Value)'';")) .....(then)
Please replace (Value) in my example with the value you want to compare without double quotes (or the variable related to the i-indexinside double quotes "+(Variable related to index i)+").
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
It is not clear from your code what you are trying to achieve - can you describe what you want to do and we can then try to assist you.
0
Sar1973Commented:
I think he's trying at each step of the loop cycle to declare the variables related to the i-index and test a condition for each of them.
0
ienaxxxCommented:
 if (holdType == 1)  

Open in new window


It will NEVER EVER enter this if, cause holdType is set as a String few rows before.

If you want to use the value of the holdType var as per the name of the var to evaluate, you need to use something like:

if(eval(holdType+'==1')) {
 htm+='X';
}

Open in new window


like Sar1973 stated, but slightly different, since the name of the var is already defined.

Anyway it's not clear.

Ah, another thing: why don't you use:

if (clientText !=null){
 //do the things here
}

Open in new window


instead of using empty case before? This can be 'cause you need to do something else in the case clientText is NULL.
0
Julian HansenCommented:
It will NEVER EVER enter this if, cause holdType is set as a String few rows before.
Agreed - hence my request for more information - we could waste time here chasing a solution based on incomplete information.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.