/    Sign up×
Community /Pin to ProfileBookmark

I’m trying to make a ‘for loop’ that will go through a string, and each time a condition is met, it will bold the character it is on (which is ‘string.charAt(loop)’).

The problem is, I don’t know how to make it do that.

First, I tried:

for (loop=0;string.length>loop;loop++)
{string.charAt(loop).bold();}

That didn’t work. Then I tried:

for (loop=0;string.length>loop;loop++)
{rep_char=string.charAt(loop).bold(); // rep_char means replacement character
string.charAt(loop)=rep_char;}

Then I realized that I was probably violating some fundamental rule of JavaScript, so I gave up and decided to ask you guys how to do it. After I tried this:

for (loop=0;string.length>loop;loop++)
{rep_char=string.charAt(loop).bold();
string=string.substring(0,loop-1)+rep_char+string.substring(loop,string.length);}

Of course that didn’t work. As you can see, I’m a beginner, and I really need some help. This is driving me nuts. Please help. Thanks.

Alan.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63Dec 19.2002 — Hey Alan,

How will you get the input string from which you want to bolde some letters and how are you displaying it once it is done?

Khalid
Copy linkTweet thisAlerts:
@AntonioMainentiauthorDec 19.2002 — Khalid -

The string is the first 1000 digits of pi, which I declared at the beginning of the JavaScript.

The idea is that you type some numbers into a textarea, and it will loop through the value of the textarea and bold the characters that do not match those in the pi string. Then it will open a little window and write the pi string and what you typed into the textarea with the errors bolded.

By the way, the reason I'm making this is to help me practice for Pi Day. There's a contest at my school to memorize the most digits, and I'm going for 1000.

Alan.
Copy linkTweet thisAlerts:
@khalidali63Dec 19.2002 — The reason that question was that I think its not possible to make characters bold sporadicaly in a text field,on the other hand if the text value is in the page then it might be achieved,

I'd written a method some time ago that parses a string and replaces a certain character,with any other character desired,I am ammeding to suite your needs and its as attached file,it will print the results under the text box.You can forward the results to a separate window or any other means for display you choose.

Khalid

[upl-file uuid=ea9a047d-8e8a-4ae2-aaed-90e5970e1ff5 size=1kB]replacecharactermethod.txt[/upl-file]
Copy linkTweet thisAlerts:
@AntonioMainentiauthorDec 20.2002 — Thanks a lot for the file you attatched. I found the flaw I made, and now that's all working, but there's another problem.

In your file, it just replaces one letter with another. In mine, it makes the letter bold, and adds <B> tags on both sides of the letter, so that when it goes through the loop again the substring method doesn't work properly because of the extra characters.

I've thought about this for a few minutes, and I can't think of a way to make it work.

If you can help me with this one, I would really really appreciate it.

Alan.
Copy linkTweet thisAlerts:
@khalidali63Dec 20.2002 — can you please explain it a bit more,and post your code here as well.

Khalid
Copy linkTweet thisAlerts:
@AntonioMainentiauthorDec 21.2002 — Sorry-

When it sets 'rep_char' equal to 'charAt(loop).bold()' it puts '<b>' tags on either side. This is a problem because when it goes through the loop the next time and sets 'a1' and 'a2' equal to substring so-and-so, it doesn't work properly because of the extra characters from the bold tags.

I thought about doing a 'split("<b>")' type thing, but I'm not sure if that would work or how I would do it exactly. I'm relatively new at this.

Thanks. Alan.
×

Success!

Help @AntonioMainenti 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.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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