increment a variable name

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
desperado
Forum Commoner
Posts: 46
Joined: Wed Dec 10, 2008 8:49 am

increment a variable name

Post by desperado »

Anyone know how I can increment a variable name within a loop?

I have:

Code: Select all

 
    var j=1;
        for (j=1;j<=4;j++){
            if (document.form1.elements['bh_hole_diam'+j].value > 0){
                var Hole+j = Math.pow((document.form1.elements['bh_hole_diam'+j].value + 0.6),2);
            }else{
                var Hole+j = 0;
            }
            if ((document.form1.elements['bh_notch_w'+j].value > 0) && (document.form1.elements['bh_notch_h'+j].value > 0)){
                var Notch+j = ((document.form1.elements['bh_notch_w'+j].value + 0.3) * (document.form1.elements['bh_notch_h'+j].value + 0.6));
            }else{
                var Notch+j = 0;
            }
        }
   
I'm trying to get results as:

Hole1=
Hole2=
Hole3=
...
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: increment a variable name

Post by kaszu »

Better use array

Code: Select all

var Hole = [];
for (j=1;j<=4;j++){
    if (...) {
        Hole[j] = ...;
    } else {
        Hole[j] = 0;
    }
    ...
}
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: increment a variable name

Post by AbraCadaver »

I agree on using arrays, but to answer the question (not tested):

1. eval('hole' + j + ' = 0');

or

2. this["hole" + j] = 0;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply