This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-08-13 17:36:59

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

random string possibility?

Is there a way in JavaScript to scramble up a string? I am coding a little project to scramble up words. I can do it with integars, but not strings so far.

What I thought might work was splitting the entered string into an array, then scramble the array, and then returning the finished string.

What I have so far:

Code:

<html><head><script>
function scramble(string) {
var chars = [string.split];
/* 
this is the scrambling part. Maybe a for loop?
*/
return chars.join("");
}
</script></head>
<body>

Field1: <input type="text" id="field1" value="Hello World!" />
<br />
Field2: <input type="text" id="field2" />
<br /><br />
Click the button to copy the content of Field1 to Field2.
<br />
<button onclick="document.getElementById('field2').value = scramble(document.getElementById('field1').value)">Scramble</button>
</body>
</html>

Hope you've got the answer.  tongue

Offline

 

#2 2011-08-13 22:48:57

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: random string possibility?

*cough*Misc*cough*


Posts: 20000 - Show all posts

Offline

 

#3 2011-08-13 23:01:06

Qwiffles
Scratcher
Registered: 2011-07-13
Posts: 100+

Re: random string possibility?

veggieman001 wrote:

*cough*Misc*cough*

Actually, haven't you browsed the Advanced Topics before?  There's more stuff then just things about scratch here!


Don't feed a pet troll.  It will turn evil.

Offline

 

#4 2011-08-14 00:07:43

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: random string possibility?

As I always say, "google it"
Well I did and I found this website that will help you. You are using JavaScript, right?


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

#5 2011-08-14 05:08:52

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: random string possibility?

GP1 wrote:

As I always say, "google it"
Well I did and I found this website that will help you. You are using JavaScript, right?

Yeah and that's PHP  tongue

Offline

 

#6 2011-08-14 05:31:31

ssss
Scratcher
Registered: 2007-07-29
Posts: 1000+

Re: random string possibility?

Visit w3schools  They're really useful!  smile


Hey.  It's me SSSS, back from the dead!  smile

Offline

 

#7 2011-08-14 05:41:30

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: random string possibility?

ssss wrote:

Visit w3schools  They're really useful!  smile

Yeah. One would hope there's a builtin command but noo.
(SSSS -Skype?)

And this is my 2222nd post!

Offline

 

#8 2011-08-14 11:27:47

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: random string possibility?

LS97 wrote:

ssss wrote:

Visit w3schools  They're really useful!  smile

Yeah. One would hope there's a builtin command but noo.
(SSSS -Skype?)

And this is my 2222nd post!

Yay, [removed]  big_smile  (didn't know if you wanted your real name shown.  tongue  )

@ssss: Yes I have. And I have looked. Eh, another 300434 more times doesn't hurt.  tongue

Last edited by Paddle2See (2011-08-14 16:36:31)

Offline

 

#9 2011-08-14 12:19:40

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: random string possibility?

LS97 wrote:

GP1 wrote:

As I always say, "google it"
Well I did and I found this website that will help you. You are using JavaScript, right?

Yeah and that's PHP  tongue

Lol, I actually thought it was ajax


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

#10 2011-08-14 12:39:09

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: random string possibility?

Hmm, I have been working on it more, but there is something wrong. The sort() is not working. Here is my current code:

Code:

<html><head><script>
function scramble(string) {
var chars = [string.split("")];
document.getElementById('scrambl').value = chars.sort();
}</script>
</head>
<body>
<div id="center">
<form>
<fieldset>
<legend><h3>Scrambl</h3></legend>
Enter:       <input type="text" size="30" id="entr"/><br />
<button onclick="scramble(document.getElementById('entr').value)" type="button">Scrambl It!</button><br/>
Scrambl'd: <input type="text" size="30" id="scrambl"/><br />

</fieldset>
</form>
</div>
</body></html>

Offline

 

#11 2011-08-14 13:02:25

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: random string possibility?

bump

Offline

 

#12 2011-08-14 17:07:15

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: random string possibility?

bump.  sad

Offline

 

#13 2011-08-15 02:48:44

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: random string possibility?

Can u give me an example of what you exactly mean by scrambling up and yes i can do that its simple  wink  just start using swap
basically everystring is a single dimension array in Javascript with indices starting from 0 instead of 1 so the array's final element is n-1 (if n i size of array)

Code:

function scramble(string){

for(var c  = 0 ; c <string.length ; c++)
{
 var swap_with = Math.floor((string.length-1) * Math.random());
 var temp = string[c];
 string[c] = string[swap_with];
 string[swap_with] = temp;
}

}

And you shall be done ^_^ .. though this might not scramble em vigrously
(maybe a while loop shall be added to change that untill it becomes unrecognizable)

such as making something like a matcher so the whole code becomes

Code:

function Match(string1,string2)
{
var matches = 0;
for(var c = 0 ; c < string1.length ; c++)
{ 
   if(string1[c]==string2[c])
   {  matches++; // 1 match!!!!
   }
   // This shall find how similar are the two strings in percentage.
}
return (matches/string1.length)*100; // Simple percentage formula 
}




function scramble(string){
var old = string.split('');
var string = string.split('');

while(Match(old,string) > 40){ // This ensures that both the string should not be with maximum tolerance being 40%

 for(var c  = 0 ; c <string.length ; c++)
  {
      var swap_with = Math.floor((string.length - 1 ) * Math.random());
       var temp = string[c];
       string[c] = string[swap_with];
       string[swap_with] = temp;
   }
  

 }

 return string.join('');
}


var String = "Hemophobia"; //  Just for testing you can remove from here.
String = scramble(String);   // Calling our function
console.log(String);            // This output can be wierdly anything.

Oh and the way you was choosing sort might  generate similar outputs ..

For me 2-3 times outa 10 it gave me recognizable Hemophobia . [ "Hmoephboia',"mhoephobia"] . to make the sort() function work you need to split the thing to array str.split('') and then run arr.sort(Math.random( ) ) ; // My solution is better though as by difficulty control (in the while loop where it checks for % of match u can input a specific hardness ^_^  )


Cheers! and keep coding!

Last edited by fanofcena (2011-08-15 02:54:44)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#14 2011-08-15 03:50:47

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: random string possibility?

Code:

<script>
function swap(ar)
{
    for(i=1;i<=100;i++) // Repeat the 'swap' 100 times
    {
        var len = ar.length // Get length
        var rand1 = Math.floor(Math.random()*len) // Select an item randomly
        var rand2 = Math.ceil(Math.random()*len) // Select another, randomly
        var primitive = ar[rand1] // 'Hold' one
        ar[rand1] = ar[rand2] // Swap them
        ar[rand2] = primitive // Swap, cont.
    }
    return ar // Return the value
}

function scramble(ar)
{
    return swap(ar).toString().replace(/,/g, "") // Swap, convert to string, then remove all commas
}
</script>

<body onload="alert(scramble(prompt('String?', 'Mississippi').split('')))"></body>

Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#15 2011-08-15 12:23:59

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: random string possibility?

Hardmath123 wrote:

Code:

<script>
function swap(ar)
{
    for(i=1;i<=100;i++) // Repeat the 'swap' 100 times
    {
        var len = ar.length // Get length
        var rand1 = Math.floor(Math.random()*len) // Select an item randomly
        var rand2 = Math.ceil(Math.random()*len) // Select another, randomly
        var primitive = ar[rand1] // 'Hold' one
        ar[rand1] = ar[rand2] // Swap them
        ar[rand2] = primitive // Swap, cont.
    }
    return ar // Return the value
}

function scramble(ar)
{
    return swap(ar).toString().replace(/,/g, "") // Swap, convert to string, then remove all commas
}
</script>

<body onload="alert(scramble(prompt('String?', 'Mississippi').split('')))"></body>

Compare it with mine  wink  mine can detect when its swapped, god knows if yours will be swapped or be a close 1 . agree ?


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#16 2011-08-15 13:24:49

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: random string possibility?

fanofcena wrote:

Can u give me an example of what you exactly mean by scrambling up and yes i can do that its simple  wink  just start using swap
basically everystring is a single dimension array in Javascript with indices starting from 0 instead of 1 so the array's final element is n-1 (if n i size of array)

Code:

function scramble(string){

for(var c  = 0 ; c <string.length ; c++)
{
 var swap_with = Math.floor((string.length-1) * Math.random());
 var temp = string[c];
 string[c] = string[swap_with];
 string[swap_with] = temp;
}

}

And you shall be done ^_^ .. though this might not scramble em vigrously
(maybe a while loop shall be added to change that untill it becomes unrecognizable)

such as making something like a matcher so the whole code becomes

Code:

function Match(string1,string2)
{
var matches = 0;
for(var c = 0 ; c < string1.length ; c++)
{ 
   if(string1[c]==string2[c])
   {  matches++; // 1 match!!!!
   }
   // This shall find how similar are the two strings in percentage.
}
return (matches/string1.length)*100; // Simple percentage formula 
}




function scramble(string){
var old = string.split('');
var string = string.split('');

while(Match(old,string) > 40){ // This ensures that both the string should not be with maximum tolerance being 40%

 for(var c  = 0 ; c <string.length ; c++)
  {
      var swap_with = Math.floor((string.length - 1 ) * Math.random());
       var temp = string[c];
       string[c] = string[swap_with];
       string[swap_with] = temp;
   }
  

 }

 return string.join('');
}


var String = "Hemophobia"; //  Just for testing you can remove from here.
String = scramble(String);   // Calling our function
console.log(String);            // This output can be wierdly anything.

Oh and the way you was choosing sort might  generate similar outputs ..

For me 2-3 times outa 10 it gave me recognizable Hemophobia . [ "Hmoephboia',"mhoephobia"] . to make the sort() function work you need to split the thing to array str.split('') and then run arr.sort(Math.random( ) ) ; // My solution is better though as by difficulty control (in the while loop where it checks for % of match u can input a specific hardness ^_^  )


Cheers! and keep coding!

Thanks!  big_smile  But what exactly is console.log supposed to do?  tongue

Offline

 

#17 2011-08-15 15:58:57

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: random string possibility?

ProgrammingFreak wrote:

fanofcena wrote:

Can u give me an example of what you exactly mean by scrambling up and yes i can do that its simple  wink  just start using swap
basically everystring is a single dimension array in Javascript with indices starting from 0 instead of 1 so the array's final element is n-1 (if n i size of array)

Code:

function scramble(string){

for(var c  = 0 ; c <string.length ; c++)
{
 var swap_with = Math.floor((string.length-1) * Math.random());
 var temp = string[c];
 string[c] = string[swap_with];
 string[swap_with] = temp;
}

}

And you shall be done ^_^ .. though this might not scramble em vigrously
(maybe a while loop shall be added to change that untill it becomes unrecognizable)

such as making something like a matcher so the whole code becomes

Code:

function Match(string1,string2)
{
var matches = 0;
for(var c = 0 ; c < string1.length ; c++)
{ 
   if(string1[c]==string2[c])
   {  matches++; // 1 match!!!!
   }
   // This shall find how similar are the two strings in percentage.
}
return (matches/string1.length)*100; // Simple percentage formula 
}




function scramble(string){
var old = string.split('');
var string = string.split('');

while(Match(old,string) > 40){ // This ensures that both the string should not be with maximum tolerance being 40%

 for(var c  = 0 ; c <string.length ; c++)
  {
      var swap_with = Math.floor((string.length - 1 ) * Math.random());
       var temp = string[c];
       string[c] = string[swap_with];
       string[swap_with] = temp;
   }
  

 }

 return string.join('');
}


var String = "Hemophobia"; //  Just for testing you can remove from here.
String = scramble(String);   // Calling our function
console.log(String);            // This output can be wierdly anything.

Oh and the way you was choosing sort might  generate similar outputs ..

For me 2-3 times outa 10 it gave me recognizable Hemophobia . [ "Hmoephboia',"mhoephobia"] . to make the sort() function work you need to split the thing to array str.split('') and then run arr.sort(Math.random( ) ) ; // My solution is better though as by difficulty control (in the while loop where it checks for % of match u can input a specific hardness ^_^  )


Cheers! and keep coding!

Thanks!  big_smile  But what exactly is console.log supposed to do?  tongue

Remove it xD .. its a function that logs output to console (wont work on internet explorer firefox/chrome ). just remove it from code i use it instead of alert to see the output.


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#18 2011-08-15 16:03:26

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: random string possibility?

fanofcena wrote:

ProgrammingFreak wrote:

fanofcena wrote:

Can u give me an example of what you exactly mean by scrambling up and yes i can do that its simple  wink  just start using swap
basically everystring is a single dimension array in Javascript with indices starting from 0 instead of 1 so the array's final element is n-1 (if n i size of array)

Code:

function scramble(string){

for(var c  = 0 ; c <string.length ; c++)
{
 var swap_with = Math.floor((string.length-1) * Math.random());
 var temp = string[c];
 string[c] = string[swap_with];
 string[swap_with] = temp;
}

}

And you shall be done ^_^ .. though this might not scramble em vigrously
(maybe a while loop shall be added to change that untill it becomes unrecognizable)

such as making something like a matcher so the whole code becomes

Code:

function Match(string1,string2)
{
var matches = 0;
for(var c = 0 ; c < string1.length ; c++)
{ 
   if(string1[c]==string2[c])
   {  matches++; // 1 match!!!!
   }
   // This shall find how similar are the two strings in percentage.
}
return (matches/string1.length)*100; // Simple percentage formula 
}




function scramble(string){
var old = string.split('');
var string = string.split('');

while(Match(old,string) > 40){ // This ensures that both the string should not be with maximum tolerance being 40%

 for(var c  = 0 ; c <string.length ; c++)
  {
      var swap_with = Math.floor((string.length - 1 ) * Math.random());
       var temp = string[c];
       string[c] = string[swap_with];
       string[swap_with] = temp;
   }
  

 }

 return string.join('');
}


var String = "Hemophobia"; //  Just for testing you can remove from here.
String = scramble(String);   // Calling our function
console.log(String);            // This output can be wierdly anything.

Oh and the way you was choosing sort might  generate similar outputs ..

For me 2-3 times outa 10 it gave me recognizable Hemophobia . [ "Hmoephboia',"mhoephobia"] . to make the sort() function work you need to split the thing to array str.split('') and then run arr.sort(Math.random( ) ) ; // My solution is better though as by difficulty control (in the while loop where it checks for % of match u can input a specific hardness ^_^  )


Cheers! and keep coding!

Thanks!  big_smile  But what exactly is console.log supposed to do?  tongue

Remove it xD .. its a function that logs output to console (wont work on internet explorer firefox/chrome ). just remove it from code i use it instead of alert to see the output.

Oh. lol. Sorry.  tongue

Offline

 

Board footer