Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

commas in array display

Status
Not open for further replies.

TFfan

Programmer
Jan 18, 2002
192
CA
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=&quot;JavaScript&quot;>
<!--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=&quot;JavaScript&quot;>


var wordArr = &quot;Next day proceed to Hawkshead; and thence by the side of Estwaite&quot;

wordArr = wordArr.split(&quot; &quot;);

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(&quot;the&quot;, &quot;and&quot;, &quot;a&quot;, &quot;is&quot;, &quot;if&quot;, &quot;of&quot;, &quot;as&quot;, &quot;in&quot;, &quot;it&quot;, &quot;its&quot;, &quot;to&quot;, &quot;are&quot;, &quot;,&quot;, &quot; &quot;, &quot;;&quot;);

for (var i=0; i < badwords.length; i++)
{
if (word == badwords)
{
return false;
}

}

return true;
}

var extract = new Array();

var y=prompt(&quot;How many words would you like to extract?&quot;);

document.write('<FORM name=&quot;form&quot;>');
document.write('<INPUT type=&quot;textbox&quot; name=&quot;text&quot; value=&quot;Press button to begin. -->&quot; size=&quot;30&quot;>');
document.write('&nbsp;&nbsp;');
document.write('<INPUT TYPE=&quot;button&quot; name=&quot;but1&quot; value=&quot;Refresh&quot; onClick=&quot;refreshtext()&quot;>');
document.write('<input type=button value=&quot;Exit&quot; onClick=&quot;javascript:window.close()&quot;>')
document.write('</FORM>');


</script>
 
Anytime you call a whole array in javascript it displays
as var[0],var[1],var[2], etc.

Try this:
<script language=&quot;JavaScript&quot;>
var test_array = new Array(&quot;one&quot;,&quot;two&quot;,&quot;three&quot;);
function disp_array() {
alert(test_array);
//this is different
alert(test_array[0] +&quot; is value index \&quot;0\&quot;&quot;);
}
</script>

2b||!2b
 
Thanks for the reply. I figured that was part of the problem, but why my code displays two commas after each word is still a mystery to me.

Also, is there a simplier way to do what I'm trying to do? The user inputs a number and the script extracts that number of random words from a paragraph. The user can then hit the &quot;refresh&quot; button and a new random set of words gets displayed.
 
Never mind, figured it out. Stupid logic slip.
 
Well, don't keep us in the dark. You won't be the last person to make that mistake, so post it up for the next guy who gets stuck on it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top