Google Answers Logo
View Question
 
Q: javascript beginners homework ( Answered,   0 Comments )
Question  
Subject: javascript beginners homework
Category: Computers > Programming
Asked by: hydro71-ga
List Price: $10.00
Posted: 18 Oct 2003 13:00 PDT
Expires: 17 Nov 2003 12:00 PST
Question ID: 267498
Write a script thta uses random number generation to create sentences.
 Use four arrays of strings called: article, noun, verb and
preposition.  Create a sentence by selecting a word at random from
each array in the following order: article, noun, verb, preposition,
article, noun.  As each word is picked, concatenate it to the previous
words in teh sentence.  The words should be separated by spaces.  When
the final sentence is output, it should start with a capital letter
and end with a period.  The program should generate 20 sentences and
output them to xhtml textarea.
Teh arrays should be filled as follows:  the article array shoudl
contain the articles: the, a, one, some and any.  The noun array
should contain the nouns: boy, girl, dog, town and car.: the verb
array should contain the verbs: drove, jumped, ran, walked, and
skipped.  The preposition array should contain the prepositions to,
from, over, under and on.
Answer  
Subject: Re: javascript beginners homework
Answered By: andyt-ga on 20 Oct 2003 20:26 PDT
 
Hi hydro71-ga,

Here is the code that does what you ask, it validates as XHTML
transitional 1.0, as requested.  If you require any clarification,
please don't hesitate to ask.  Most of this was done without research,
but a trememndous resouce for javascript is located at the internet
related technologies page for javascript.  They have articles
http://tech.irt.org/articles/script.htm and an extensive FAQ
(http://developer.irt.org/script/script.htm).

Regards,
andyt-ga

HTML Attachment---------------------


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<title>javascript for ga</title>

<script type="javascript">
function win_onload()	{
var uarticle= new Array(5) 
uarticle[0]="The"; 
uarticle[1]="A"; 
uarticle[2]="One"; 
uarticle[3]="Some"; 
uarticle[4]="Any"; 

var larticle= new Array(5) 
larticle[0]="the"; 
larticle[1]="a"; 
larticle[2]="one"; 
larticle[3]="some"; 
larticle[4]="any"; 

var noun= new Array(5) 
noun[0]="boy"; 
noun[1]="girl"; 
noun[2]="dog"; 
noun[3]="town"; 
noun[4]="car"; 

var verb= new Array(5) 
verb[0]="drove"; 
verb[1]="jumped"; 
verb[2]="ran"; 
verb[3]="walked"; 
verb[4]="skipped"; 

var preposition= new Array(5) 
preposition[0]="to"; 
preposition[1]="from"; 
preposition[2]="over"; 
preposition[3]="under"; 
preposition[4]="on"; 

string=""
for(start = 0; start < 20; start++) {
var randomuarticle=Math.floor(Math.random()*5) 
var randomnoun=Math.floor(Math.random()*5) 
var randomverb=Math.floor(Math.random()*5) 
var randompreposition=Math.floor(Math.random()*5) 
var randomlarticle=Math.floor(Math.random()*5) 
var randomnoun2=Math.floor(Math.random()*5) 
string= string+ uarticle[randomuarticle]  + " " + noun[randomnoun] +"
" + verb[randomverb] + " " + preposition[randompreposition] + " " +
larticle[randomlarticle] + " " + noun[randomnoun2] + "." +"\n";

window.document.form1.text1.value=string
}
}



</script>
</head>
<body onload="win_onload()"> 
<form name="form1" action="">
<p><textarea name="text1" rows="25" cols="40" >
</textarea></p>
</form>


</body>
</html>

Request for Answer Clarification by hydro71-ga on 23 Oct 2003 10:47 PDT
It doesn't work, I pasted the code into notepad and saved it as a
.html.  I just get a blank text area with a scroll bar.

thanks

Tim

Clarification of Answer by andyt-ga on 24 Oct 2003 06:58 PDT
Hello again,
I've put the code up at:
http://www.greenrobot.net/ga/javascript_for_ga.html

Just save it as an html, and you should be all set.  If it continues
to not function, please leave your browser and operating system and
we'll work it out.

Regards,
andyt-ga

Clarification of Answer by andyt-ga on 24 Oct 2003 11:04 PDT
Hi,
I tested the script on with IE on windows, and indeed there is nothing
but a blank textarea.  That wasn't the case on my machine.  I'm trying
to figure out what's wrong, but wanted to let you know what's up.

Regards,
andyt-ga

Clarification of Answer by andyt-ga on 24 Oct 2003 11:27 PDT
Ok, The script at http://www.greenrobot.net/ga/javascript_for_ga.html
is now working.  Sorry for any inconvenience.
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy