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

I can't get my code to work. Random Quote Generator

I've been pulling my hair trying to figure this project out. I honestly need help. I can't get my quote to print. I honestly don't know if my random quote function is working.

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="css/styles.css"
  </head>
  <body>
    <div class="container"></div>
    <script src="js/script.js"></script>
  </body>
</html>
//quotes array
var quotes = [
  {
    quote: "Do the difficult things while they are easy and do the great things while they are small. A journey of a thousand miles must begin with a single step.",
    source: "-Lao Tzu",
  },
  {
    quote: "Behind every successful man is a woman rolling her eyes.",
    source: "-Jim Carrey",
  },
  {
    quote: "To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment.",
    source: "-Ralph Waldo Emerson",
  },
  {
    quote: "Look deep into nature, and then you will understand everything better.",
    source: "-Albert Einstein",
  },
  {
    quote: "If you could kick the person in the pants responsible for most of your trouble, you wouldn’t sit for a month.",
    source: "-Theodore Roosevelt",
  }

];

//array for seen quotes
var seenQuotes = [];
var randomQuote;

//function for randomizing quotes
function getRandomQuote() {
  Math.floor(Math.random() * quotes.length) = randomQuote;
  if (quotes.length == 0) {
      seenQuotes = quotes;
  }
};

//function to print quote
function printQuote() {
  var clickQuote = getRandomQuote();
  console.log(clickQuote);
  printText = '<p class="quote">' + clickQuote.quote + '</p>' + '<p class="source">' + clickQuote.source + '</p>'
  document.getElementById('quote-box').innerHTML = printText;
};

3 Answers

Hi Jorge,

A few items we need to fix for your quote to print:

getRandomQuote function:

  • fix use of = operator -- value being assigned to variable needs to go on the right when using the assignment operator.
// value should go on the right side when using '=' operator
Math.floor(Math.random() * quotes.length) = randomQuote;
  • accessing quotes array -- you are generating a random index but are not using it to access your quotes array
// this gets you a random number but we still need to use it with the quotes array ( ex: quotes[0] )
Math.floor(Math.random() * quotes.length)
  • missing return statement -- your function is set up to generate the quote object but does not return it.

Looks like based on the JS code you provided the HTML might be outdated(printQuote is calling document.getElementById('quote-box') but doesn't exist in HTML), so I made some tweaks to get a working example. Note: It does contain the whole solution so if you are looking to avoid spoilers, hopefully the hints above are enough. Good luck!

Thank you for the help! It was actually very simple. Now I'm getting the euro symbol and "a" with an arrow on top in the beginning and end of the quote. Any idea what that might be?

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,863 Points

Hi Jorge,

Without looking through all your code, you are missing a closing angle bracket for the CSS include in your HTML <head> section.

<link rel="stylesheet" href="css/styles.css">  <!-- added a closing bracket for this line -->

See if that fixes the problem.

:) :dizzy:

Thank you! But unfortunately it still didn't fix my problem. I appreciate the help.

Some of your code has spaces in some functions that shouldn't be there. Javascript is really strict when writing formulas.

hint function(){

}

same with your if statements.