Hi. I'm trying to display a user-defined number of random words from a paragraph in a text box (doesn't necessarily have to be a textbox) and everything seems to work except that instead of a word or two it displays commas. For instance if I set the number of words to 5, it displays word1,,word2,,word3. Being new to javascript I have no idea. I works fine if I use a series of document.write_s and integers (I'm using this to display the array: document.form.text.value=(extract)
.
Thanks!
<SCRIPT language="JavaScript">
<!--hide
function refreshtext()
{
for (var i=0; i < y; i++)
{
var rand=Math.floor(Math.random()*goodArr.length);
extract = goodArr[rand];
i++;
}
document.form.text.value=(extract);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<script language="JavaScript">
var wordArr = "Next day proceed to Hawkshead; and thence by the side of Estwaite"
wordArr = wordArr.split(" "
;
var goodArr = new Array();
var k=0;
for (var i=0; i<wordArr.length; i++)
{
if (isGoodWord(wordArr))
{
goodArr[k] = wordArr;
k++;
}
}
function isGoodWord(word)
{
var badwords = new Array("the", "and", "a", "is", "if", "of", "as", "in", "it", "its", "to", "are", ",", " ", ";"
;
for (var i=0; i < badwords.length; i++)
{
if (word == badwords)
{
return false;
}
}
return true;
}
var extract = new Array();
var y=prompt("How many words would you like to extract?"
;
document.write('<FORM name="form">');
document.write('<INPUT type="textbox" name="text" value="Press button to begin. -->" size="30">');
document.write(' ');
document.write('<INPUT TYPE="button" name="but1" value="Refresh" onClick="refreshtext()">');
document.write('<input type=button value="Exit" onClick="javascript:window.close()">')
document.write('</FORM>');
</script>
Thanks!
<SCRIPT language="JavaScript">
<!--hide
function refreshtext()
{
for (var i=0; i < y; i++)
{
var rand=Math.floor(Math.random()*goodArr.length);
extract = goodArr[rand];
i++;
}
document.form.text.value=(extract);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<script language="JavaScript">
var wordArr = "Next day proceed to Hawkshead; and thence by the side of Estwaite"
wordArr = wordArr.split(" "
var goodArr = new Array();
var k=0;
for (var i=0; i<wordArr.length; i++)
{
if (isGoodWord(wordArr))
{
goodArr[k] = wordArr;
k++;
}
}
function isGoodWord(word)
{
var badwords = new Array("the", "and", "a", "is", "if", "of", "as", "in", "it", "its", "to", "are", ",", " ", ";"
for (var i=0; i < badwords.length; i++)
{
if (word == badwords)
{
return false;
}
}
return true;
}
var extract = new Array();
var y=prompt("How many words would you like to extract?"
document.write('<FORM name="form">');
document.write('<INPUT type="textbox" name="text" value="Press button to begin. -->" size="30">');
document.write(' ');
document.write('<INPUT TYPE="button" name="but1" value="Refresh" onClick="refreshtext()">');
document.write('<input type=button value="Exit" onClick="javascript:window.close()">')
document.write('</FORM>');
</script>