Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Random sentence generator using array

    I am taking a Javascript class and the teacher assigned this:

    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.

    I haven't gotten to the loop yet, I'm just working on the random sentence first. Here is what I have:

    Code:
     <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>

    Am I on the right track? How would I loop the sentences using a for statement?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You should use a for loop similarly to the one used in Fig. 8.6 in order to get it to print 20 times
    Did the dog eat figure 8.6?


    Code:
    var str=[], 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"];
    
    Array.prototype.rand=function(){
    	var i= Math.floor(Math.random()*this.length);
    	return this[i];
    }
    
    for(var i=0;i<20;i++){
        str[str.length]= [uarticle.rand(),noun.rand(),verb.rand(),
        preposition.rand(),larticle.rand(),noun.rand()+ "."].join(' '); 
    }
    document.write(str.join('<br>'));
    Last edited by mrhoo; 03-29-2010 at 05:40 PM. Reason: pity

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    As is so often the case, mrhoo's solution is far too advanced for a student or beginner.

    Your script can be improved by generating two different random numbers for the two nouns, to avoid (even more) meaningless sentences.

    Note that the random numbers are simple variables, not an array!

    Code:
    <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"); 
    
    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 ); 
    var rand5 = Math.floor ( Math.random() * preposition.length );
    
    document.write(uarticle[rand2] + " " + noun[rand2] + " " + verb[rand3] + " " + preposition[rand1] + " " + larticle[rand4] + " " + noun[rand2a] + ". <br>"); 
    
    }
    
    </script>

    The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997).

    A historian and a psychologist are sitting outside at a nudist colony. Historian: "Have you read Marx?" Psychologist: "Yes, I think they're from the wicker chairs."

  • #4
    New Coder
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry I forgot to include that, kind of important huh? Here is Figure 8.6

    Code:
          <style type = "text/css">
             table { width: 15em }
             th    { text-align: left }
          </style>
          <script type = "text/javascript">        
             <!--
             var face; 
             var frequency = [ , 0, 0, 0, 0, 0, 0 ]; // leave frequency[0] 
                                                     // uninitialized
    
             // summarize results
             for ( var roll = 1; roll <= 6000; ++roll )    
             {                                             
                face = Math.floor( 1 + Math.random() * 6 );
                ++frequency[ face ];                       
             } // end for                                             
    
             document.writeln( "<table border = \"1\"><thead>" );
             document.writeln( "<th>Face</th>" +  
                "<th>Frequency</th></thead><tbody>" );
                         
             // generate entire table of frequencies for each face
             for ( face = 1; face < frequency.length; ++face )      
                document.writeln( "<tr><td>" + face + "</td><td>" + 
                   frequency[ face ] + "</td></tr>" );              
         
             document.writeln( "</tbody></table>" );
             // -->
          </script>

  • #5
    New Coder
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you Philip M for your help, (you too mrhoo, but I have to agree that its a bit advanced for me right now).

    I had the comment tags in there just because the teacher likes them there.

    Also thank you for throwing that while statement in there to stop the double noun sentences.
    Last edited by harofreak00; 03-29-2010 at 06:14 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by harofreak00 View Post
    Also thank you for throwing that while statement in there to stop the double noun sentences.
    Your teacher should not teach ancient and obsolete stuff!

    You can do the same with uarticle and larticle:-

    Code:
    var rand4 = Math.floor ( Math.random() * larticle.length ); 
    while (rand4 == rand1) {
    rand4 = Math.floor ( Math.random() * larticle.length ); 
    }

  • #7
    New Coder
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Alright, so here is what I've got to turn in. Thanks again!

    Code:
    <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>

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    I don't think that satisfies the requirement that you use output similar to "figure 8.6".

    I think the instructor is inferring he wants to see an HTML <table> with <tr> rows, etc.

    Trivial to change what you have to do that, of course.

    And it's just me, but I would have used more meaningful names for you random number variables:
    Code:
    for (var i = 1; i <=20; i++) {
        var uarticle_rand = Math.floor ( Math.random() * uarticle.length ); 
        var noun1_rand = Math.floor ( Math.random() * noun.length ); 
        var noun2_rand = Math.floor ( Math.random() * noun.length ); 
        ... etc. ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Old Pedant View Post
    I don't think that satisfies the requirement that you use output similar to "figure 8.6".

    I think the instructor is inferring he wants to see an HTML <table> with <tr> rows, etc.

    Trivial to change what you have to do that, of course.

    I don' see that, as the output of this exercise is not a table with columns. The instruction is "You should use a for loop similarly to the one used in Fig. 8.6 in order to get it to print 20 times."

    N.B. infer != imply.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Sorry...started to write "I think you should infer...".

    I no bettor then that.

    <grin style="sickly" />

    I guess I'd ask the instructor, were I the student.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •