1

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));
2
  • What is the question? Commented Dec 21, 2022 at 17:03
  • I want to use text-transform property for random letters in string, output should be like this "shOAiB alI KhaN SoOmRO" Commented Dec 21, 2022 at 17:07

1 Answer 1

1

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"));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.