Question

Write a script that 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 and noun. 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 arrays should be filled as follows: the article array 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. The program should generate 20 sentences to form a short story and output the result to an HTML5 textarea. The story should begin with a line reading Once upon a time... and end with a line reading THE END.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code Image:

<!DOCTYPE html> <html> <head> <!-- title--> <title>Random Sentence Creator</title> <!-- call java script--> <script type = t//Implementation of loadPage function function loadPage() var shortStoryline = Once upon a time. ..\n; 7/Declare number as// Implementation of generateRandomSentence function function generateRandomSentence() 11 11 11 7/Declare sentence as type of// Implementation of randomWordGen function function randomWordGen(result) //Declare randValue as type of var var randValue =Sample Output:

Once upon a time. .. Some dog skipped to some car. A boy jumped from a girl. The car ran over a car. The dog ran under any doCode to copy:

<!DOCTYPE html>

<html>

   <head>

   <!-- title-->

      <title>Random Sentence Creator</title>

   <!-- call java script-->

      <script type = "text/javascript">

         //Declare article as array assign those words to article

         var article = ["the", "a", "one", "some", "any"];

        //Declare noun as array assign those words to noun

        var noun = ["boy", "girl", "dog", "town", "car"];

        //Declare verb as array assign those words to verb

         var verb = ["drove", "jumped", "ran", "walked", "skipped"];

        //Declare preposition as array assign those words to preposition

         var preposition = ["to", "from", "over", "under", "on"];

        //Implementation of loadPage function

         function loadPage()

         {

             var shortStoryline = "Once upon a time. ..\n";

            

             //Declare number as type of var

             var number = 20;

            

             //Iterate the loop

             for (var k = 1; k <= number; k++)

            

             //create shortStoryline

             shortStoryline = shortStoryline + generateRandomSentence();

            

             //add shortStoryline

             shortStoryline = shortStoryline + "\nTHE END.";

             //shortStorylineField.value

             document.getElementById("shortStorylineField").value = shortStoryline;

        }

        

         // Implementation of generateRandomSentence function

         function generateRandomSentence()

         {

             //Declare sentence as type of var

             var sentence = capitalWord(randomWordGen(article)) + " " + randomWordGen(noun)

                            + " " + randomWordGen(verb) + " " + randomWordGen(preposition)

                            + " " + randomWordGen(article) + " " + randomWordGen(noun) + ". ";

             return sentence;

         }

       

         // Implementation of capitalWord function

         function capitalWord(firstwordofsentence)

         {

             //get the capital letter

             var capitalLetter = firstwordofsentence.charAt(0);

             //convert into capitalLetter

             capitalLetter = capitalLetter.toUpperCase();

             //get the sub string

             var sub = firstwordofsentence.substr(1);

             //calculate firstwordofsentence

             firstwordofsentence = capitalLetter + sub;

             return firstwordofsentence;

        }

         

         // Implementation of randomWordGen function

         function randomWordGen(result)

         {

             //Declare randValue as type of var

             var randValue = Math.random() * result.length;

             //Declare resultIndex as type of var

             var resultIndex = Math.floor(randValue);

             return result[ resultIndex ];

         }

      </script>

   </head>

   <body onload = "loadPage()">

      <form action = "">

        <!-- paragraph-->

         <p><textarea id = "shortStorylineField" cols = "100" rows = "8"></textarea></p>

      </form>

      </body

</html>

Add a comment
Know the answer?
Add Answer to:
Write a script that uses random number generation to create sentences. Use four arrays of strings...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that uses the random number generator to create sentences. The program should use...

    Write a program that uses the random number generator to create sentences. The program should use four arrays of pointers to char called article, noun, verb and preposition. The sentences are constructed in the following order: article, noun, verb, preposition, article, noun. The program should generate 20 sentences. Capitalize the first letter. The arrays should contain: article "the", "one", "a", "some", "any" noun "boy", "girl", "dog", "town", "car" verb "drove", "jumped", "walked", "ran", "skipped" preposition "to", "from", "over", "under", "on"...

  • Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm...

    Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm Objective This assignment will consist of writing a program that involves practice with arrays, random number generation, and Strings. You may use c-strings or string objects here, however, string objects is recommended. Exercise Filename: sentences.cpp Write a program that uses random-number generation to create sentences. Create four arrays of strings (string objects highly suggested over c-strings) called article, noun, verb, and preposition. The arrays...

  • Can somebody help me with Java programming? please be brief and explain the process and carefully...

    Can somebody help me with Java programming? please be brief and explain the process and carefully follow the step by step instruction. Thanks Homework 12.1 - Write a program that generates five random sentences (like mad libs), prints them to a file, then reads in the sentences and prints them to the console. Start by creating four string arrays for nouns, verbs, colors, and places. They should store at least 5 of each type of word. You can have more...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT