Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Thanked 0 Times in 0 Posts

    Question Random Array help

    Hi, I'm pretty new to JS and have been trying to get a random array code to work. I have looked over my script multiple times and cannot seem to find what's wrong with the script. I'm hoping someone more experience can help me find what I'm doing wrong. Thanks

     <SCRIPT language="JavaScript">
    function get_random()
        var ranNum= Math.floor(Math.random()*8);
        return ranNum;
    function getaQuote()
       var whichQuote=get_random();
        var quote=new Array(8)
         quote[0]="An expletive used by knowledgeable film buffs during any chase scene involving a foreign or ethnic locale, reflecting their certainty that a fruit cart will be overturned during the chase, and an angry peddler will run into the middle of the street to shake his fist at the hero\'s departing vehicle.";
    	quote[1]="In any horror film involving a human transforming into a monster, the hero never has to explain to the police the naked dead human body that is left after he has killed the monster. This despite the fact that no one in authority ever believes in the monster in the first place.";
    	quote[2]="Any stolen clothing or shoes will perfectly fit any male character whether they were stolen from a clothesline or removed from a Nazi guard, police officer, lookout, etc., who was overpowered and whose identity the clothing thief has now assumed";
    	quote[3]="In horror films that feature a mad scientist there is always a scene where the scientist explains the rational for his bizarre experiments. After the explanation, the scientist will always turn to his guest and say, 'You think I\'m mad, don\'t you?'”;
    	quote[4]="If there is more than one or two of an alien race, they are always roughly the same size as humans.";
    	quote[5]="Aliens usually speak english and have same colloquialisms. planet";
    	quote[6]="All members of alien species wear the same outfits, including clothing, hairstyles, and jewelery. This makes them readily identifiable. Aliens who do not dress like aliens are hiding something.";
    	quote[7]="This may, in fact, be a consequence of the fact that aliens all have single, monolithic cultures: one language, one religion, one outfit, per planet.";
    <FORM name="form1">
    <INPUT TYPE="button" value="Get a Quote!" onClick="getaQuote()">

  2. #2
    Join Date
    Apr 2009
    Thanked 426 Times in 424 Posts
    You're obviously learning from an outdated source, which isn't great, but what's really tripping your script up is the fact that for quote[3] you're using instead of " as closing quote.
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  3. #3
    New to the CF scene
    Join Date
    Feb 2012
    Thanked 0 Times in 0 Posts
    Thanks! I don't know how many times I went over my code looking for small mistakes like that. Just needed another pair of eyes I guess. Thanks so much, maybe I can find an updated way of doing it now that I know what was going wrong.


Tags for this Thread

Posting Permissions

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