/    Sign up×
Community /Pin to ProfileBookmark

Help with Javascript function

Hi guys,

I am quite new to Javascript but enjoying what I’m doing. I was hoping some people more knowledgeable than myself could help me out with this function I’m trying to write:

function addSpaces(aString)
{

for (var i =0; i < aString.length; i = i + 1)
{
document.write(aString.charAt(i)+’ ‘);
}
}

document.write(addSpaces(‘Testing’)) ;

How do I define the last character in the string and/or disregard it when the string is displayed? Thanks in advance for your responses.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@DebaserauthorSep 19.2009 — Double post
Copy linkTweet thisAlerts:
@justinbarneskinSep 20.2009 — [code=html]<SCRIPT type="text/javascript">
function addSpaces(aString){ var x=''
for(i=0; i<aString.length;i++){
i<aString.length-1 ? x+=aString.charAt(i)+' ': x+=aString.charAt(i);}
document.getElementById('showIt').innerHTML=x;
}
</SCRIPT>

<input onchange="addSpaces(this.value)">
<div id="showIt"></div>[/code]
Copy linkTweet thisAlerts:
@mrhooSep 20.2009 — Another way-
[CODE]function addSpaces(s){
return s.split('').join(' ');
}[/CODE]

//test

alert(addSpaces('Testing'));
Copy linkTweet thisAlerts:
@DebaserauthorSep 20.2009 — Thank you very much to both justinbarneskin and mrhoo, your help has proven very helpful indeed.
×

Success!

Help @Debaser 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...