Replacing characters in strings.

admin

Administrator
Staff member
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,scroll
bar,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.
 
Top