[Javascript] variable undefined problem

Rodney Myers rodney at aflyingstart.net
Sat May 24 04:10:27 CDT 2003


Hello,

As the while loop starts with a valid itemCheck variable it should cycle 
until the last valid variable.
But inside the loop you have

x = "item" + i;
itemCheck =eval(x);


if itemN was the last valid item, and so permitted entry to the loop
the N+1 is guaranteed to be undefined.

hth

Rodney


hypersdc at cox.net wrote:

>I'm having problems with a variable undefined error.
>
>I'm grabbing a set of variables from a file (.js file). I'm cycling through
>the variables with a while statement. The problem I'm having is the last
>variable is getting an undefined. How can I fix the code so this doesn't
>happen.
>
>//Start Code:
>
>var i = 0;
>var itemCheck = item0;
>while(window.itemCheck)
>{
>
>  document.write("<A HREF=" + itemCheck + ">" + itemCheck + "<BR></A>" );
>
>   i++;
>x = "item" + i;
>itemCheck =eval(x);
>
>if (typeof eval(x) == 'undefined')
>    alert('2 Undefined');
>else
>    alert('2 okay');
>itemCheck =eval(x);
>
>
>}
>
>
>//End code
>
>//js content
>var item0 = "name 1";
>var item1 = "name 2";
>
>//end js file content
>
>Thanks in advance
>
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript
>
>  
>

-- 

PS. I now have resale rights for Mike Chen's
BizAutomator. If you have to answer routine 
email queries from your sites or mailings then
you will find this simple-to-use program invaluable.
http://www.BizAutomator.co.uk










More information about the Javascript mailing list