You have to know more rigorously data type. replace function is operating on string data type. Your a1 is somewhat an integer. In this case, why not multiply by (-1) to make it positive?! If you are at a school, you've to ask your tutor.
[tt]
//a1=a1*(-1);
//or if you want a contrived twist
a1=parseInt(a1.toString().replace(/-/,""),10);
[/tt]