Random Quote. Help!

Random Quote. Help!
0

#1


I don’t know what to fix.


#2

So, the output is “undefined”. As a debugger, my first instinct is to find out if it is indeed undefined. I looked at your getQuote() function.

  function getQuote(){
    //var colors = ['green', 'red', 'yellow', 'purple', 'grey', 'blue', 'black', 'pink'];

    var quotes = [ /* quote defs redacted for brevity */ ];
    //var fonts = ['Bangers','Gloria Hallelujah','Indie Flower','Permanent Marker','Rock Salt'];

    var quote = $('#text').text();
    var quoteLength = quotes.length;
    //var color = $('$colors').color();
    //var font = $('font-family').
    var randomNumberQuotes = Math.floor(Math.random()*quoteLength);
    //var randomNumberColors = Math.floor(Math.random()*colors.length);   
    //var randomNumberFonts = Math.floor(Math.random()*fonts.length);

    for (i=0; i<=quoteLength; i+=1) {
      var newQuote = quotes[randomNumberQuotes].quote;
    }

    var timeAnimation = 500;
    var quoteText = $('#text');
    quoteText.fadeOut(timeAnimation, function(){
      quoteText.html('');
      console.log(newQuote)
      quoteText.append('<span>' + newQuote + '</span>');
      quoteText.fadeIn(timeAnimation);
    });

  }

Right before you send newQuote to the screen, I console.log it. When I open the browser console, I see that newQuote is indeed `undefined*.

So I look where it is assigned, in the for loop.

   for (i=0; i<=quoteLength; i+=1) {
      var newQuote = quotes[randomNumberQuotes].quote;
    }

There are a couple of problems here. First of all, why a for loop? You only need to assign one quote, the quote at the index determined by randomNumberQuotes.

So, all you need is:

var newQuote = quotes[randomNumberQuotes].quote;

But why .quote at the end? This isn’t an object, it returns a string. If you remove that. Your code works.

var newQuote = quotes[randomNumberQuotes];

Please go though the steps I did so you can learn how to debug. Make sure you understand why these changes do what they do.

Let us know if it doesn’t work or if anything is unclear.


#3

i think that will be help u.
all u need is just remove for looping and
add this line.

var newQuote = quotes[randomNumberQuotes];


#4

Thanks a lot! It’s works.
I think i understand how you did.
I wanted to do with the change color and font but realized that was too hard for me yet:)


#5

You need to review some step in js like Object and Array that are real complicated part.

coding answer is like this;
var newColor = quotes[randomNumberColors];
var newFonts = quotes[randomNumberFonts];

Keep Clam and Try again ; dude :robot::robot:


#6

As you started to help me let’s continue :slight_smile:
I need to change $colors in Sass on the random colors from the arrey.


I was able to do so :slight_smile:


#7

I’m sorry.
I made a mistake and got the wrong answer. Just noticed. Thanks for the parse error. Really helped.