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

Jayson Camacho
seal-mask
.a{fill-rule:evenodd;}techdegree
Jayson Camacho
Full Stack JavaScript Techdegree Student 3,006 Points

Can't seem to get the button to print the quote.

This is what I have so far I think the problem I'm having is with my printQuote it's at the end of the code.

// event listener to respond to "Show another quote" button clicks // when user clicks anywhere on the button, the "printQuote" function is called

var quotes = [ { quote : 'At the end of the day, you’ve gotta feel some way. So why not feel unbeatable? Why not feel untouchable?', source : 'Conor McGregor' }, { quote : 'All that we are is a result of what we have thought.', source : 'Buddha' }, { quote : 'Everything you want is out there waiting for you to ask. Everything you want also wants you. But you have to take action to get it.', source : 'Jack Canfield' }, { quote : 'It’s really important that you feel good. Because this feeling good is what goes out as a signal into the universe and starts to attract more of itself to you. So the more you can feel good, the more you will attract the things that help you feel good and that will keep bringing you up higher and higher.', source : 'Joe Vitale' }, { quote : 'A person is what he or she thinks about all day long.', source : 'Ralph Waldo Emerson' }, { quote : 'Your imagination is your preview of life’s coming attractions.', source : 'Albert Einstein' }, { quote : 'Nothing external to me has any power over me.', source : 'Walt Whitman' }, { quote : 'Happiness is not a destination. It is a method of life.', source : 'Burton Hills' }, { quote : 'Doubt is only removed by action. If you’re not working then that’s where doubt comes in.', source : 'Conor McGregor' } ];

var quote; var source;

function getRandomQuote() { var sourceLength = quotes.length; var randomNumber = Math.floor(Math.random()*sourceLength); for (var i = 0; i <= sourceLength; i += 1) { var newQuoteText = quotes[randomNumber].quote; var newQuoter = quotes[randomNumber].source; var leeQuote = newQuoteText + newQuoter; console.log(leeQuote); return; } }

document.getElementById('loadQuote').addEventListener("click", printQuote, false);

function printQuote(message) { document.getElementById('quote-box').innerHTML

}

var message = getRandomQuote(); printQuote(message);

Steven Parker
Steven Parker
231,127 Points

Proper code quoting would help tremendously. Please see the Markdown Cheatsheet link at the bottom of the "Add an Answer" section for tips on how to post code to the Community. :arrow_heading_down:

Even better, make a snapshot of your workspace and post the link to it here.

3 Answers

Steven Parker
Steven Parker
231,127 Points

It's hard to tell without proper code formatting...

And I had to guess at the HTML, but it seems like the code has a few errors, including:

  • in getRandomQuote, the quote is logged to the console but is not returned
  • in printQuote, the assignment of message is missing

Once these are fixed, the quote will appear initially, but the button will display mouse event information instead of a new quote. This is because printQuote requires a message argument, but an event handler is passed an event object as the argument.

One way to fix that would be to use a function that needs no argument to display a random quote as the handler:

document.getElementById('loadQuote').addEventListener("click", printRandomQuote, false);

function printRandomQuote() {
  document.getElementById('quote-box').innerHTML = getRandomQuote();
}

In future questions, remember to quote your code or post a link to a workspace snapshot.

Steven Parker
Steven Parker
231,127 Points

Do we still need the snapshot, or has your question been fully answered?

Also, there's just one snapshot. It includes all code and data files.