Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code Cleanup and Possible Array

Status
Not open for further replies.

bobbybrown69

Programmer
Jun 11, 2003
15
US
How can I clean this up (possibly using an array) that would take into effect all instances of l != 2 - 12

i.e. If l != 6 then oNewTd6 - oNewTd12 should = document.createElement("");
i.e. If l != 9 then oNewTd9 - oNewTd12 should = document.createElement("");

New to javascript and not sure how to clean this up and make it work.
-----------------------------------

if (l != 2){
oNewTd2 = document.createElement("");
}
if (l != 3){
oNewTd3 = document.createElement("");
}
if (l != 4){
oNewTd4 = document.createElement("");
}
if (l != 5){
oNewTd5 = document.createElement("");
}
if (l != 6){
oNewTd6 = document.createElement("");
}
if (l != 7){
oNewTd7 = document.createElement("");
}
if (l != 8){
oNewTd8 = document.createElement("");
}
if (l != 9){
oNewTd9 = document.createElement("");
}
if (l != 10){
oNewTd10 = document.createElement("");
}
if (l != 11){
oNewTd11 = document.createElement("");
}
if (l != 12){
oNewTd12 = document.createElement("");
}
 
[tt]var someArray = new Array(null, null, oNewTd2, oNewTd3, oNewTd4, oNewTd5, oNewTd6, oNewTd7, oNewTd8, oNewTd9, oNewTd10, oNewTd11, oNewTd12);
someArray[l] = document.createElement("");[/tt]

But it begs the question... why would anybody ever wish to create empty elements... and since you're performing the same action every time, why would you even bother to check?

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
There is too much code to paste in but I am doing a for loop to add an integer 2-12 to the end of oNewTd depending on the value of l

I will test the array you gave me. Thanks.
--------------------------------------
for(var l=0, field, elm; field=lblFieldOrder[l++];) {
oNewTd = "";
if (field == "Make"){
oNewTd = eval("oNewTd"+(l+1));
oNewTd2 = document.createElement("td");
}
...
...
...
...
}
 
I get a type mismatch error when using your code.

var someArray = new Array(null, null, oNewTd2, oNewTd3, oNewTd4, oNewTd5, oNewTd6, oNewTd7, oNewTd8, oNewTd9, oNewTd10, oNewTd11, oNewTd12);
someArray[l] = document.createElement("");

But this works.
if (l != 12){
oNewTd12 = document.createElement("");
}

Any ideas why I am getting the error when using the array?
 
Are you declaring the variables prior to putting them in the array?

Also the first two elements of the array are null. Meaning that if you start your loop at 0 instead of 2 as you indicated in your first post, it won't work.

Perhaps if you told us what you were trying to achieve we might have more chance of actually providing you with something resembling a sensible answer.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Sometimes after this for loop m will be 4, 5, 6 and up to 12. I need to know if after the loop if the value of l is > 2 then oNewTd2 to oNewTd12 should = document.createElement("");

For example if l = 6 then oNewTd7, oNewTd8, oNewTd9, oNewTd10, oNewTd11, oNewTd12 need to = document.createElement("");

------------------------
for(var m=0, fields, elms; fields=lblFieldOrder[m++];) {
var oNewTd = "";
if ((fields == "Year4") || (fields == "Year2")){
if (linerYear4 != ""){
oNewTd = eval("oNewTd"+(m+1));
oNewTd4 = document.createElement("td");
}
if (linerYear2 != ""){
oNewTd = eval("oNewTd"+(m+1));
oNewTd4 = document.createElement("td");
}
}
if (fields == "Make"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd2 = document.createElement("td");
}
if (fields == "Model"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd3 = document.createElement("td");
}
if (fields == "Series"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd7 = document.createElement("td");
}
if (fields == "BodyStyle"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd9 = document.createElement("td");
}
if (fields == "VINFull"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd = document.createElement("td");
}
if (fields == "VINLast8"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd6 = document.createElement("td");
}
if (fields == "Miles"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd10 = document.createElement("td");
}
if (fields == "RetailPrice"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd8 = document.createElement("td");
}
if (fields == "InternetPrice"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd11 = document.createElement("td");
}
if (fields == "StockNum"){
oNewTd = eval("oNewTd"+(m+1));
oNewTd5 = document.createElement("td");
}
}

if (l != 2){
oNewTd2 = document.createElement("");
}
if (l != 3){
oNewTd3 = document.createElement("");
}
if (l != 4){
oNewTd4 = document.createElement("");
}
if (l != 5){
oNewTd5 = document.createElement("");
}
etc.
etc.
etc.
if (l != 11){
oNewT1 = document.createElement("");
}
if (l != 12){
oNewTd12 = document.createElement("");
}
 
>[tt][blue]Code Cleanup[/blue] and Possible Array[/tt]
Start with using indent.
As to
[tt]>if (l != 2){
>oNewTd2 = document.createElement("");
>}
>etc.
>etc.
>etc.
>if (l != 12){
>oNewTd12 = document.createElement("");
>}
[/tt]it is this.[tt]
for (var i=l+1;i<=12;i++) {
someArray=document.createElement("");
}[/tt]
The rest, yours explanation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top