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:
<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.
Offline
As I always say, "google it"
Well I did and I found this website that will help you. You are using JavaScript, right?
Offline
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
Offline
ssss wrote:
Visit w3schools They're really useful!
![]()
Yeah. One would hope there's a builtin command but noo.
(SSSS -Skype?)
And this is my 2222nd post!
Offline
LS97 wrote:
ssss wrote:
Visit w3schools They're really useful!
![]()
Yeah. One would hope there's a builtin command but noo.
(SSSS -Skype?)
And this is my 2222nd post!
Yay, [removed]
(didn't know if you wanted your real name shown.
)
@ssss: Yes I have. And I have looked. Eh, another 300434 more times doesn't hurt.
Last edited by Paddle2See (2011-08-14 16:36:31)
Offline
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
![]()
Lol, I actually thought it was ajax
Offline
Hmm, I have been working on it more, but there is something wrong. The sort() is not working. Here is my current 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
Can u give me an example of what you exactly mean by scrambling up and yes i can do that its simple
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)
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
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)
Offline
<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>Offline
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
mine can detect when its swapped, god knows if yours will be swapped or be a close 1 . agree ?
Offline
fanofcena wrote:
Can u give me an example of what you exactly mean by scrambling up and yes i can do that its simple
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 becomesCode:
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!
But what exactly is console.log supposed to do?
Offline
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
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 becomesCode:
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!
But what exactly is console.log supposed to do?
![]()
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.
Offline
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
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 becomesCode:
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!
But what exactly is console.log supposed to do?
![]()
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.
Offline