Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

transform characters randomly in javascript

I want to transform text randomly in javascript. e.g: raNDomlYrAnDfomR

const characters = "shoaib ali khan soomro";

  function generateString(length) {
    let result = " ";
    const charactersLength = characters.length;
    for (let i = 0; i < length; i++) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }

    return result;
  }
  let length = characters.length;
  console.log(generateString(length));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For ASCII, you can XOR the character code with 32 to switch the case.

function generateString(str) {
  return [...str].map(c => Math.random() < .5 ? c : 
    String.fromCharCode(c.charCodeAt() ^ 32)).join('');
}
console.log(generateString("shoaib ali khan soomro"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading