/    Sign up×
Community /Pin to ProfileBookmark

Padding Values in Array using For Loops

I am trying to make each word in an array have the same number of characters….so I add blank spaces to the end of the words so that each word in a ‘column’ has the same length.

Here is the code i have put together so far:

[code]
function padWord(aArray,tc){
var sFMTD=””;

for (c=0; c < tc; c++){
var mLen=0;
for (r=0; r < 11; r++){
if (aArray[r][c].length > mLen) mLen=aArray[r][c].length;
}
for (r=0; r < 11; r++){
if (aArray[r][c].length < mLen){
for (a=0; a < (mLen-aArray[r][c].length); a++){
aArray[r][c]+=” “;
}
}
}
}

for (r=0; r < 11; r++){
if (sFMTD!=””) sFMTD+=”n”;
for (c=0; c < tc; c++){
sFMTD+=aArray[r][c]+” “;
}
}
return sFMTD;
}
[/code]

But, its not working right…any ideas?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@steveaAug 03.2007 — A quick glance says that it probably works, my guess is that the problem comes in when you're putting it into HTML. HTML doesn't like whitespace, and will ignore them after a certain amount (2 or 3 maybe). Perhaps you'd be better off substituting [b]&nbsp;[/b] where you add " ".
Copy linkTweet thisAlerts:
@jeffcravenerauthorAug 03.2007 — Hmm...i tried replacing the " " with &nbsp;

Getting a syntax error...so i tried it with quotes around, and then it just adds the &nbsp; not a space
Copy linkTweet thisAlerts:
@jeffcravenerauthorAug 03.2007 — Some background...it puts the sFMTD variable's value into a variable defined on the HTML page, then sends that over in the post action to a mail tempalte form that uses plain text.
Copy linkTweet thisAlerts:
@Arty_EffemAug 03.2007 — Some background...it puts the sFMTD variable's value into a variable defined on the HTML page, then sends that over in the post action to a mail tempalte form that uses plain text.[/QUOTE]Then you could try adding non-breaking spaces as text, not HTML:

element+="xA0";
Copy linkTweet thisAlerts:
@jeffcravenerauthorAug 03.2007 — Thanks...that did create the spaces, but still running into the same problem as just using the " "....it doesn't seem to be creating enough...as if my frunction's for loop is off somehow.
×

Success!

Help @jeffcravener spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...