Replace random chars in a string in javascript - Javascript String Operation

Javascript examples for String Operation:String Replace

Description

Replace random chars in a string in javascript

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <script type="text/javascript">
var ctu = [],s = "this is a test this is a test.", newstring=" ";
ctu[0]="A";/*w w w  .j a  v  a2 s.co m*/
ctu[1]="B";
ctu[2]="C";
var s = s.split(" ");
for(var i = 0;i<s.length;i++){
  s[i]=ctu[Math.floor(Math.random() * ctu.length)];
  newstring += s[i] + " ";
}
document.write(newstring);

      </script>  
   </body>
</html>

Related Tutorials