Fragment string into random parts

Asked

Viewed 81 times

2

I need a JS script that fragments any string into random parts, let’s say I have a string:

x = "Deliciosogostoeobomgostodasnuvensseremfeitasdealgodao"

and after inserted in the script, it can be returned like this for example:

"Del icios ogos t oeobo mgosto das nuvensser emf eitasde a lgod ao"
  • Ever tried to do something? If so, enter the code, explain it, and describe the problems encountered.

  • I found a code and changed it but it does not separate right, always missing some letter var str = 'Deliciosogostoeobomgostodasnuvensseremfeitasdealgodao';&#xA;var chunks = [];&#xA;&#xA;for (var i = 0, charsLength = str.length; i < charsLength; i += (Math.floor(Math.random()*10)+1)) {&#xA; chunks.push(str.substring(i, i + (Math.floor(Math.random()*10)+1)));&#xA;}&#xA;console.log(chunks.toString().replace(/\,/g," "));

2 answers

3

I created a function that does this job, however, I advise you to test and optimize it before applying in a real case, even more when the string is too large. I would do this on the server-side and send the processed string to the client-side.

Behold:

function dividirStrPor(str, caractereDivisao, pedacos)
{
  var tam = str.length;

  for (var i = 0; i < pedacos; i++)
  {
    posicao = Math.floor(Math.random() * (tam - 0 + 1) + 0);

    str = str.slice(0, posicao) + caractereDivisao + str.slice(posicao);
  }

  return str;
}


var str = "Deliciosogostoeobomgostodasnuvensseremfeitasdealgodao";

var resultado = dividirStrPor(str, " ", 12);

console.log(resultado);

Exit:

D e lic iosogos t oeob omgostodas nuvensseremfe i t asdealgodao

The output changes at each function call due to the random position at which the split character will be inserted into the string.

See working on repl it..

Sources:

Javascript: How can I Insert a string at a specific index
Generate Random number between two Numbers in Javascript

2


You can achieve this by breaking the string into parts with a maximum of 9 characters (this you can change in the code you have 9) and adding to an array and then joining everything with join:

Example:

var x = "Deliciosogostoeobomgostodasnuvensseremfeitasdealgodao",
    x_novo = [];
while(x.length > 0){
   var rn1 = Math.floor(Math.random()*9)+1,
       parte = x.substring( x.charAt(0), rn1 );
   x_novo.push(parte);
   x = x.replace(parte,'');
}

x_novo = x_novo.join(" ");
console.log(x_novo);

Example without using array:

var x = "Deliciosogostoeobomgostodasnuvensseremfeitasdealgodao",
    x_novo = '';
while(x.length > 0){
   var rn1 = Math.floor(Math.random()*9)+1,
   parte = x.substring(x.charAt(0), rn1);
   x_novo += (x_novo.length > 0 ? ' ' : '')+parte;
   x = x.replace(parte, '');
}

console.log(x_novo);

  • 1

    I have a slight disagreement as to how pedaco (in the second part, not the first) was removed from the original string. I have reservations about the fact that the replace can take a substring identical to the chunk but occurring before it, so it can not generate hypothetical substrings that could be generated if the appropriate chunk was pulled. But, finally, caveats only, I did not seek to validate my concern, nor should this purist concern withdraw the merit of the answer

  • 1

    You’re right. I didn’t think of that. I’ll look into that. Even because the second answer is not even part of the question, I put only bonus. Really can make a difference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.