HipHop Quote Generator

HipHop Quote Generator
0.0 0

#1

I’m working on the random quote generator, and I’m having trouble with getting a new random quote to appear when the button is clicked. When I console.log I see that it is generating the same quote over and over. My code is as followed:

//   Defining new quotes to be uesd
var newQuotes = [
  "It was all a dream/ I used to read Word Up! magazine/Salt-N-Peppa and Heavy D up in the limosine...",
  "Funny how money change a situation/miscommunication leads to complication...",
  "I came though the door, I said it before/I never let the mic magnetize me no more/but it's biting me, fighting me, inviting me to ryme/I can't hold it back, I'm looking for the line...",
  "I never knew a l-l-love like this/gotta be something for me to write this...",
  "My dear, my dear, my dear, you do not know me but I know you very well/ Now let me tell you about the feelings I have for you/ When I try, or make some sort of attempt, I symp/ Damn I wish I wasn't such a wimp...",
  "...Dead in the middle of Little Italy, little did we know,that we riddled two middlemen who didn't do diddily...",
  "I see no changes all I see is racist faces/ Misplaced hate makes disgrace to races/ We under I wonder what it takes to make this/ One better place, let's erase the wasted...",
  "Just think, what if you could just, just blink yourself away?/ Just think, what if you could just, just blink yourself away?/ Jeff X can rock the mic with tooth decay/ I be the 5 foot 7, residing at the Mecca, rest address in south section...",
  "...Now who's these kings of these rude ludicrous lucrative lyrics/ Who could inherit the title, put the youth in hysterics...",
  "...I'm representin' for the seat where Rosa Parks sat/ Where Malcolm X was shot, where Martin Luther was popped/ So off we go, let the trumpets blow/ And hold on, because the driver of the mission is a pro-The ruler's back"
];

// generating a random number
var randomNumber = Math.floor(Math.random() * newQuotes.length);
// defining random quotes
var randomQuote = newQuotes[randomNumber];
// making the random quote appear when newQuote button is clicked
$("#newQuote").on('click', function() {
  for (var i = 0; i < newQuotes.length; i++) {
    console.log(randomQuote);
  }
});

Knowing me it could be something simple like a missing semicolon or something, but any help will be greatly appreciated.


#2

In the above code, you assign a specific quote from the newQuotes array to randomQuote. Since your console.log(randomQuote) refers to the same variable (randomQuote) in the for loop, then you are going to display the same quote multiple times.

If you want a different quote to show in the console each time the button is clicked, then you need to get rid of the for loop and simply generate a new randomNumber and that number to refer to a different quote in new Quotes array. Basically, you need to move the to var statements inside of your button click function.


#3

Thanks Randell! That worked perfectly!