-1

My question shouldn't be too difficult but I haven't solved it yet. Basically, what I'm trying to do is to take a message (like this one), preserve each letter in the message, but generate a random message using each letter. So, I can currently read into a textbox (say) "Hello!", but I need to take the message in that textbox and (on the click of a button) have something like "lolH!e". There's got to be a simple way to read each letter into an array (or list, or whatever), and spit them out at random, but while using each letter only once as in the original message. Any thoughts?

  • 2
    What language are you using? – Bijay Gurung Nov 17 '16 at 08:29
  • Terrible title. And "should not be too difficult" is really not any info that we need. Please read the help pages on how to ask good questions. I recommend bringing your questions *to the point*, meaning that you clearly divide in "assumptions", "problem", "approach", mark these parts and derive a *precise* question from that. – Marcus Müller Feb 28 '18 at 22:26

1 Answers1

0

In JavaScript you can do something like this:

function randomize(s){
    var a = Array.from(s);
    for(var j, x, i = a.length; i; j = parseInt(Math.random() * i), x = a[--i], a[i] = a[j], a[j] = x);
    return a.join("");
}

Then use it like:

randomize("Hello!")

There are some other good solutions here: How do I shuffle the characters in a string in JavaScript?

Community
  • 1
  • 1
phobia82
  • 1,257
  • 8
  • 10