Click to See Complete Forum and Search --> : randomizing a word
doobster
05-05-2003, 08:51 PM
Hello,
I have a form which contains two fields. The first one accepts a word. The second one is the word scrambled. Currently I have to manually scramble the word.
What I would like to do is after the first field looses focus that it can call a javascript function that automatically scrambles that word.
i.e.
apple -> palpe
Help would be appreciated.
Post your current code.
<input type=text onblur="scramble()"> would be an example of an onblur function.
doobster
05-05-2003, 09:25 PM
Sorry, I know how to call the function. But I need the code for the function :D
OK, well, do you have any code already? Or are we doing this from scratch?
SniperX
05-06-2003, 12:16 AM
Hi -
I'm not too sure, but this might work...
function scramble {
var someText = document.form.inputName.value;
var index=0;
var tempString;
usedIndexes = new Array();
for (var i=0; i<someText.length(); i++) {
index=Math.random() * someText.length();
//check if the index has not already been used - if it has, generate a new one....
tempString[i] = someText[index];
}
//then let the value in the textbox = the generated value...
document.form.inputName.value = tempString;
}
Don't know if that will work - i am but a lowly C/C++ programmer...
Regards MW
SniperX, I changed your code a little and came up with this. Doesn't work right, but I think it's a start...
<html><head><script>
function sink(){
var someText = document.x.l.value;
var tempString = new Array(someText.length);
for(var i=0;i<someText.length;i++)
index=Math.round(Math.random()*someText.length);
tempString[i] = someText.charAt(index);
document.x.l.value = tempString;
}
</script></head>
<body><form name="x">
<input type=text name="l"><br>
<input type=button onclick="sink()" value="Scramble">
</form></body></html>
SniperX
05-06-2003, 12:58 AM
Don't worry about changing my code, its da only way to learn
doobster
05-06-2003, 08:59 AM
Hi guys, thanks for the replies.
I tested both code and it kind of just hangs and never puts any values in. I will show you what I have for code.
<script language="Javascript">
<!--
function scramble() {
var someText = document.addword.word.value;
var index=0;
var tempString;
for(var i=0;i<someText.length;i++)
index=Math.round(Math.random()*someText.length);
tempString[i] = someText.charAt(index);
}
//then let the value in the textbox = the generated value...
document.addword.letters.value = tempString;
}
//-->
</script>
<form action=admin.php?cmd=addword method=post name=addword>
<input type=hidden name=form value=sent>
<table border=0>
<tr>
<td><b>Five Letter Word: </b></td>
<td><input type=text name=word maxlength=5></td>
</tr>
<tr>
<td><b>Five Random Letters From Word: </b></td>
<td><input type=text name=letters maxlength=5></td>
</tr>
<tr>
<td><input type=submit value="Add Word"></td>
<td><input type=button onClick="scramble()" value="Scramble">
</tr>
</table>
</form>
Looks, great. We just have to work on the Javascript part now. Anyhow, I've created my own 'scramble' script that I worked on for a few minutes, but it doesn't work yet...
I know this thread is really old, but I figured I'd revive it and provide a solution, since Doobster never replied saying he found one, and I never provided one. So here is working code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Strict 1.0//EN"
"http://www.w3.org/TR/xhtml1/dtd/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JavaScript Example</title>
</head>
<body>
<h1>JavaScript Example</h1>
<script type="text/javascript">
/**********************************************************/
/*Copyright Jonathan Fenocchi */
/* http://cmm.sonoracabinets.com/jona/index.php */
/* or http://cmm.rgabbard.com/ */
/* Do not use without direct permission from Jonathan */
/**********************************************************/
function get(data){
queryStr = location.search;
queryStr = queryStr.split(data+"=")[1];
queryStr = (queryStr.indexOf("&"))?queryStr.split("&")[0]:queryStr;
return unescape(queryStr);
}
var str = get('str');
if(str.indexOf(' ') != -1){
var strs = str.split(" ");
for(j=0; j<strs.length; j++){
var str = strs[j];
var len = str.length;
var ary = new Array(len);
var rand = 0;
for(i=0; i<len; i++){
rand = Math.floor(Math.random()*str.length);
if(ary[rand] != rand){
document.write (str[rand]);
ary[rand] = rand;
} else {
len++;
}
}
document.write (" ");
}
} else {
var len = str.length;
var ary = new Array(len);
var rand = 0;
for(i=0; i<len; i++){
rand = Math.floor(Math.random()*str.length);
if(ary[rand] != rand){
document.write(str[rand]);
ary[rand] = rand;
} else {
len++;
}
}
}
</script>
</body>
</html>