Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Random Quote generator; Cannot get my second function to work properlly

https://w.trhou.se/wtgoadblev

Can someone review my code? I am having trouble getting it to function properly. I was getting it to work in the console, but then when I made the second function to print the quote to the page, it just comes out undefined.

3 Answers

There's an error on line 62. It looks like your getRandomQuote func is returning a single quote and not the entire quote object.

On line 63 your var randomQuote = "A single quote" and not { quote: , source, citation, year} object. I would start with making sure your getRandomQuote function returns the whole quote object.

Hey Karly, Looks like you're ok until lines 62 and 63 with these lines of code. Here you are assigning a string, the actual quote returned by the getRandomQuote function to the value of the randomQuote variable.

let randomQuote = getRandomQuote();

Then in the next lines of code you attempt to use the string as an index into value for the quotes array, and this is where your problems begin:

let quote = quotes[randomQuote].quote;

Hope this helps...

This may help.

function getRandomQuote(){
  let ranNumber = Math.floor(Math.random()*quotes.length);

  // quote.innerHTML = '<span>"</span>' + quotes[ranNumber].quote+''+'<span>"</span>';
  let randomQuote= quotes[ranNumber];
  return randomQuote;
}

/***
 * `printQuote` function
***/
function printQuote()  {
  //initialize and set a value for the variables listed below
  let html = '';
  //call the getRandomQuote for a list of quotes and properties in the array of objects
  let randomQuote = getRandomQuote();
  let quote = randomQuote.quote;
  let source = randomQuote.source;
  let citation = randomQuote.citation;
  let year = randomQuote.year;