Forum Moderators: open
Write a script that uses a random number generation to create sentences and name it sentences.html. Use five arrays of strings called: uppercase article (uarticle), noun, verb, lowercase article (larticle), and preposition. You will need to use the correct case for the article arrays.
Create a sentence by selecting a word at random from each array in the following order: uarticle, noun, verb, preposition, larticle, noun. You can find examples of generating random numbers in both Fig. 8.6 (dice-rolling) and Fig. 8.7 (random image) of Chapter 8.
The arrays should be filled at minimum, as follows: the article array(s) should 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. If you would like to add more words, adjust the arrays appropriately.
As each word is picked, concatenate it to the previous words in the 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 the screen (document.write or document.writeln). You should use a for loop similarly to the one used in Fig. 8.6 in order to get it to print 20 times.
<script type="text/javascript">
<!--
uarticle = new Array("The", "A", "One", "Some", "Any");
noun = new Array("boy", "girl", "dog", "town", "car");
verb = new Array("drove", "jumped", "ran", "walked", "skipped");
larticle = new Array("the", "a", "one", "some", "any");
preposition = new Array("to", "from", "over", "under", "on");
var rand1 = [Math.floor ( Math.random() * uarticle.length )];
var rand2 = [Math.floor ( Math.random() * noun.length )];
var rand3 = [Math.floor ( Math.random() * verb.length )];
var rand4 = [Math.floor ( Math.random() * larticle.length )];
var rand5 = [Math.floor ( Math.random() * preposition.length )];
document.write(uarticle[rand2] + " " + noun[rand2] + " " + verb[rand3] + " " + preposition[rand1] + " " + larticle[rand4] + " " + noun[rand2] + ".");
-->
</script>
for (initialization; condition; update) {
statements
}
for (i = 0, n = 20; i < n; i++) {
// statements
}
<script type="text/javascript">
var uarticle = new Array("The", "A", "One", "Some", "Any");
var noun = new Array("boy", "girl", "dog", "town", "car");
var verb = new Array("drove", "jumped", "ran", "walked", "skipped");
var larticle = new Array("the", "a", "one", "some", "any");
var preposition = new Array("to", "from", "over", "under", "on");
for (var i = 1; i <=20; i++) {
var rand1 = Math.floor ( Math.random() * uarticle.length );
var rand2 = Math.floor ( Math.random() * noun.length );
var rand2a = Math.floor ( Math.random() * noun.length );
while (rand2 == rand2a) {rand2a = Math.floor ( Math.random() * noun.length ); }
var rand3 = Math.floor ( Math.random() * verb.length );
var rand4 = Math.floor ( Math.random() * larticle.length );
while (rand4 == rand1) {rand4 = Math.floor ( Math.random() * larticle.length ); }
var rand5 = Math.floor ( Math.random() * preposition.length );
var space = " ";
document.write(uarticle[rand2] + space + noun[rand2] + space + verb[rand3] + space + preposition[rand1] + space + larticle[rand4] + space + noun[rand2a] + ". <br>");
}
</script>
<script type="text/javascript">
var i,
uarticle = ["The", "A", "One", "Some", "Any"],
noun = ["boy", "girl", "dog", "town", "car"],
verb = ["drove", "jumped", "ran", "walked", "skipped"],
larticle = ["the", "a", "one", "some", "any"],
preposition = ["to", "from", "over", "under", "on"],
rand1,
rand2,
rand2a,
rand3,
rand4,
rand5,
space = " ";
for (i = 1; i <=20; i++) {
rand1 = Math.floor ( Math.random() * uarticle.length );
rand2 = Math.floor ( Math.random() * noun.length );
rand2a = Math.floor ( Math.random() * noun.length );
while (rand2 == rand2a) {
rand2a = Math.floor ( Math.random() * noun.length );
}
rand3 = Math.floor ( Math.random() * verb.length );
rand4 = Math.floor ( Math.random() * larticle.length );
while (rand4 == rand1) {
rand4 = Math.floor ( Math.random() * larticle.length );
}
rand5 = Math.floor ( Math.random() * preposition.length );
document.write(uarticle[rand1] + // not rand2
space + noun[rand2] +
space + verb[rand3] +
space + preposition[rand5] + // not rand1
space + larticle[rand4] +
space + noun[rand2a] + ". <br>");
}
</script>
<script type="text/javascript">
var i,
uarticle = ["The", "A", "One", "Some", "Any"],
noun = ["boy", "girl", "dog", "town", "car"],
verb = ["drove", "jumped", "ran", "walked", "skipped"],
larticle = ["the", "a", "one", "some", "any"],
preposition = ["to", "from", "over", "under", "on"],
uarticleIndex,
nounIndex1,
nounIndex2,
verbIndex,
larticleIndex,
prepositionIndex,
space = " ";
for (i = 1; i <=20; i++) {
uarticleIndex = Math.floor ( Math.random() * uarticle.length );
nounIndex1 = Math.floor ( Math.random() * noun.length );
nounIndex2 = Math.floor ( Math.random() * noun.length );
while (nounIndex1 == nounIndex2) {
nounIndex2 = Math.floor ( Math.random() * noun.length );
}
verbIndex = Math.floor ( Math.random() * verb.length );
larticleIndex = Math.floor ( Math.random() * larticle.length );
while (larticleIndex == uarticleIndex) {
larticleIndex = Math.floor ( Math.random() * larticle.length );
}
prepositionIndex = Math.floor ( Math.random() * preposition.length );
document.write(uarticle[uarticleIndex] +
space + noun[nounIndex1] +
space + verb[verbIndex] +
space + preposition[prepositionIndex] +
space + larticle[larticleIndex] +
space + noun[nounIndex2] + ". <br>");
}
</script>