Random Quote Generator Stopping at Random Clicks?

Hello,

Yesterday I started the Random Quote Generator task on CodePen. It’s generating the quotes just fine on the click of the button, but then it stops at random clicks. I’ve counted, and it’s pretty much a different click that it stops at every time. Any ideas on why it’s stopping would be much appreciated! Here’s the code (please ignore the quotes; I’m revising for English exams and I thought I’d kill two birds with one stone):

quotes = [
  {
      quote: '"...dominion giv\'n / Over all other creatures that possess / Earth, air, and sea."',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },

  {
      quote: '"Let us ever praise him and extol / His bounty, following our delightful task / to prune these growing plants and tend these flow\'rs, / Which were it toilsome, yet with thee were sweet."', 
      
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"God hath set / Labour and rest, as day and night to men / successive."',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"Branches overgrown / That mock our scant manuring, and require / more hands than ours to lop their wanton growth."',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"Let us divide our labours, thou where choice / Leads thee, or where most needs."',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"He for God only, she for God in him."',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"She as a veil down to her tender waist / Her unadorned golden tresses wore, / Dishevelled, but in wanton wringlets waved."',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"Pleased it returned as soon with answering looks, / Of sympathy and love, there I had fixed / Mine eyes til now, and pined with vain desire / Had not a voice thus warned me"',
      
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"On the Tree of Life, / The middle tree and highest there that grew, / Sat like a cormorant."',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"A steep wilderness, whose hairy sides / With thicket overgrown, grotesque and wild, / Access denied."',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"The verduous wall of Paradise"',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"High on a throne of royal state, which far / Outshone the wealth of Ormus and of Ind, / Or where the gorgeous East with richest hand / shower\'s on her kings barbaric pearl and gold / Satan exalted sat."',
    
      author: '<em>John Milton, Paradise Lost</em>'
  },
  
  {
      quote: '"What dire Offense from am\'rous Causes springs, / What great Contests arise from trivial Things, / I sing."',
    
      author: '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"Now meet thy fate, incens\'d Belinda cried, / And drew a deadly Bodkin from her Side, / \(The same, his ancient Personage to deck, / Her great great Grandsire wore about his Neck\)."',
    
      author:  '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"A Fan, a Garter, half a Pair of Gloves, / And all the trophies of his former Loves."',
    
      author: '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"Whether the nymph shall break Diana\'s Law, / Or some frail china jar receive a flaw, / Or stain her honour or her new brocade."',
    
      author:  '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"Oh had\'st thou, Cruel! been content to seize / Hairs less in sight, or any hairs but these!"',
    
      author: '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"Unnumbered Treasures ope at once, and here / The various Off\'rings of the World appear."',
    
      author: '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"This Casket India\'s glowing Gems unlocks, / And all Arabia breathes from yonder Box."',
    
      author: '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"On shining Altars of Japan they raise / The silver lamp; the fiery spirits blaze. / From silver spouts the grateful Liquors glide, / While China\'s earth receives the smoaking tyde."',
    
      author:  '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"One speaks the glory of the British Queen, / And one describes a charming Indian screen."',
    
      author:  '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"I sing - This verse, to Caryll, Muse! is due."',
    
      author: '<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"Fair Tresses, Man\'s Imperial Race ensnare, / And beauty draws us with a single hair."',
    
      author:'<em>Alexander Pope, The Rape of the Lock</em>'
  },
  
  {
      quote: '"A constant vapour o\'er the palace flies, / Strange phantoms with the rising mists arise, / Dreadful as hermit\'s dreams in haunted shades, / Or bright as visions of expiring maids. / Now glaring fiends and snakes on rolling spires, / Pale spectres, gaping tombs and purple fires."',
    
      author: '<em>Alexander Pope, The Rape of the Lock</em>'
  }
]

$(document).ready(function(){
  
  var a = Math.floor(Math.random() * 25) - 1;
  
  $('#quoteButton').on('click', function(){
    $('#quote').html(quotes[a].quote  + ' - ' + quotes[a].author);
    a = Math.floor(Math.random() * 25) - 1;
    });
});

Your random formula is off. The formula:

a = Math.floor(Math.random() * 25) - 1;

will generate a number from -1 and 23. You want:

a = Math.floor(Math.random() * 25) ;

to generate a number from 0 to 24 (correct because arrays are zero indexed).

Your code was occasionally calling the -1 cell, which doesn’t exist.

2 Likes

Don’t use “magic numbers”

$(document).ready(function(){
  
  var a = Math.floor(Math.random() * quotes.length);
  
  $('#quoteButton').on('click', function(){
    $('#quote').html(quotes[a].quote  + ' - ' + quotes[a].author);
    a = Math.floor(Math.random() * quotes.length) ;
    console.log(a);
    });
});
1 Like

Thank you! I combined your recommendation with owel’s suggestion to change the number to ‘quotes.length’ and it’s now working fine!

Good point by Owel.

Also, the first random calculation isn’t really necessary if you move the second one one line earlier.

And if you wrap all those two lines (the var a=Math and $.(“quote”)) in function, you can call it when you first enter the program.

Thank you! I was going to look into that but you saved me what would probably have been a long time figuring it out!