Jump to content

Clean Up Repetitive Coding


Deejtko

Recommended Posts

Bellow is a rule I pieced together from some other post. It worked for what I needed but I would like to learn how to clean something like this up and make it more efficient. So if you are bored and good at coding some help would be greatly appreciated.

 

Thank you,

 

var StrLine1 = (!Field("series_1")) ?" ": Trim(Field("series_1") + "<br>");
var StrLine2 = (!Field("series_2")) ?" ": Trim(Field("series_2") + "<br>");
var StrLine3 = (!Field("series_3")) ?" ": Trim(Field("series_3") + "<br>");
var StrLine4 = (!Field("series_4")) ?" ": Trim(Field("series_4") + "<br>");
var StrLine5 = (!Field("series_5")) ?" ": Trim(Field("series_5") + "<br>");
var StrLine6 = (!Field("series_6")) ?" ": Trim(Field("series_6") + "<br>");
var StrLine7 = (!Field("series_7")) ?" ": Trim(Field("series_7") + "<br>");
var StrLine8 = (!Field("series_8")) ?" ": Trim(Field("series_8") + "<br>");
var StrLine9 = (!Field("series_9")) ?" ": Trim(Field("series_9") + "<br>");
var StrLine10 = (!Field("series_10")) ?" ": Trim(Field("series_10") + "<br>");
var StrLine11 = (!Field("series_11")) ?" ": Trim(Field("series_11") + "<br>");
var StrLine12 = (!Field("series_12")) ?" ": Trim(Field("series_12") + "<br>");
var StrLine13 = (!Field("series_13")) ?" ": Trim(Field("series_13") + "<br>");
var StrLine14 = (!Field("series_14")) ?" ": Trim(Field("series_14") + "<br>");
var StrLine15 = (!Field("series_15")) ?" ": Trim(Field("series_15") + "<br>");
var StrLine16 = (!Field("series_16")) ?" ": Trim(Field("series_16") + "<br>");

return StrLine1 + StrLine2 + StrLine3 + StrLine4 + StrLine5 + StrLine6 + StrLine7 + StrLine8 + StrLine9 + StrLine10 + StrLine11 + StrLine12 + StrLine13 + StrLine14 + StrLine15 + StrLine16;

Link to comment
Share on other sites

Replacing the repetition of it with a loop, and a join.

var Strline = [];
for (i=1; i <= 16; i++){
Strline[i] = (!Field("series_" + i)) ?" ": Trim(Field("series_" + i) + "<br>");
}

return Strline.join("");

 

 

Replacing even more with a filter and adjusting the join.

var Strline = [];
for (i=1; i <= 16; i++){
Strline[i] = Trim(Field("series_" + i));
}

return Strline.filter(String).join("<br>");

 

I'm sure there's ways to make it even simpler. But, I tend to leave code a little more verbose.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...