3
function scramble(a){a=a.split("");for(var b=a.length-1;0<b;b--){var c=Math.floor(Math.random()*(b+1));d=a[b];a[b]=a[c];a[c]=d}return a.join("")}

I've got this code, which seems to be effective in scrambling a single word by calling an alert:

alert(scramble('Like this.'));

Here's what I'm trying to do though: I want to be able to enter text in a textarea, separated by newlines, and randomly scramble each string line by line. For example:

testing
scramble
words

Would output something like:

sgnitte
rceamslb
dwros

Can anyone help me in doing this?

Craig
  • 563
  • 1
  • 6
  • 18
  • Best way to do this: [How to randomize (shuffle) a JavaScript array? - Stack Overflow](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array/18650169#18650169) – eapo Oct 12 '20 at 09:26

3 Answers3

4

function shuffle(str) {
  var str = document.getElementById('txt');
  var a = str.innerHTML;
  var newArr = [];
  var neww = '';
  var text = a.replace(/[\r\n]/g, '').split(' ');
  
  text.map(function(v) {
    v.split('').map(function() {
      var hash = Math.floor(Math.random() * v.length);
      neww += v[hash];
      v = v.replace(v.charAt(hash), '');
    });
    newArr.push(neww);
    neww = '';
  });
  var x = newArr.map(v => v.split('').join(' ')).join('\n');
  str.value = x.split('').map(v => v.toUpperCase()).join('');
}
<textarea cols='60' rows='8' id="txt">testing &#13;&#10;scramble &#13;&#10;words</textarea>
<button onclick='shuffle()'>Shuffle</button>
kind user
  • 40,029
  • 7
  • 67
  • 77
2

Try this code:

function scramble(a){a=a.split("");for(var b=a.length-1;0<b;b--){var c=Math.floor(Math.random()*(b+1));d=a[b];a[b]=a[c];a[c]=d}return a.join("")}

function scrambleText(){
var textArea = document.getElementById('TEXTAREA_ID');
var lines = textArea.value.split('\n');
for(var i = 0;i < lines.length;i++){
    lines[i] = scramble(lines[i]).toUpperCase().split('').join(' ');
}
textArea.value = lines.join('\n');
}

First you get the text area element, then you get its value and split that into an array by line. You can then scramble each line individually with your existing function. Lastly, all there is to do is to convert the array back to a string and get the scrambled text back into the text area.

EDIT: You can use the toUpperCase method to transform all characters to upper case. The combination of split join, like you can see in the code above, can be used to add spaces between the characters.

JSFiddle: https://jsfiddle.net/NotABlueWhale/u8wjuz1r/7/

NotABlueWhale
  • 795
  • 7
  • 18
  • This worked perfectly. You're very fast. Additionally, do you know how I am make it so that it converts everything to uppercase letters and puts a space between each letter converted? Example input: testing scramble words Example output: S G N I T T E R C E A M S L B D W R O S – Craig Feb 20 '17 at 00:58
  • @Kinduser You're the best. Thanks! – Craig Feb 20 '17 at 20:45
1

function Scamble (s,shift){
 var r = "";
 for(var i = 0; i < s.length; i++){
   r += String.fromCharCode(s.charCodeAt(i) + shift)
 }
 return r
}

alert(Scamble ("abcd",33324)) //returns '芍芎芏芐'
alert(Scamble ('芍芎芏芐',-33324)) //returns abcd
Igor Krupitsky
  • 787
  • 6
  • 9