Random sentence generator using array

Discussion in 'JavaScript' started by harofreak00, Mar 29, 2010.

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


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

     <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> 
    Code (markup):

    Am I on the right track? How would I loop the sentences using a for statement?
     
    harofreak00, Mar 29, 2010 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    yeah sure. this is nice.

    I'd code it differently for maintainability mostly:
    
    (function(count) {
        var words = {
            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"]
        };
        
        // how it's structured (object keys will be used)
        var sentence = ["uarticle","noun", "verb", "preposition", "larticle", "noun"];
    
        // return a random array
        var arrayRandom = function(myarray) {
            return myarray[Math.floor ( Math.random() * myarray.length)];
        };
        
        // a temp array to store the selected random words.
        var readySentence = [];
        
        // loop it
        while(count--) {
            readySentence.length = 0; // empty the array on each loop
            for (var ii = 0; ii < sentence.length; ++ii) {
                readySentence.push(arrayRandom(words[sentence[ii]]));
            };
            document.writeln(readySentence.join(" ") +"<br/>");
        }
    })(20);
    
    Code (javascript):
    the reason why this is better is when you decide to add more words, groups of words or change the sentence structure, you do not need to change anything else in the code.

    here it is running:
    http://www.jsfiddle.net/42tKq/1/

    not the cleverest stuff it came up with though:
    good luck :)
     
    dimitar christoff, Mar 29, 2010 IP