/    Sign up×
Community /Pin to ProfileBookmark

Replacing characters in strings.

I am trying to write a JavaScript function that compares a string that I declared as a variable with what the user types into a textarea.

What I want it to do is make the characters that don’t match bold and write it on a little popup window so they can see what they did wrong.

So what I did was I made a loop, and for each character it compares the “charAt(loop)” of each string, and if they’re not the same, the variable “rep_char” (replacement character) is set equal to “a.charAt(loop).bold()” (‘a’ being the string), and it sets “a.charAt(loop)” equal to “rep_char”.

This is what it looks like:


——————————

function checkEntry(a) // ‘a’ is the value of the textarea
{

for (loop=0;a.length>loop;loop++)
{

if (a.charAt(loop)!=pi.charAt(loop))
{

rep_char=a.charAt(loop).bold();
a=a.substring(0,loop-1)+rep_char+a.substring(loop+1,a.length);
num_wrong++;

}

}

new_window=open(”,”,’height=400,width=800,scrollbar,resizable’);
new_window.document.write(a+”<br><br>Your entry contained “+num_wrong+” errors.”); );

}


——————————

But apparently you’re not allowed to tell it to change a character in a string to something else.

I’m guessing that comparing strings and bolding the characters that aren’t the same is a standard technique in JavaScript. But I’m new to this, so I don’t know it. Can anyone tell me how I should do this? Thanks.

Alan.

to post a comment
JavaScript

0Be the first to comment 😎

×

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,
)...