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

Will Hunting
Will Hunting
13,726 Points

How can I construct strings using the different properties of the quote object in my code ?

var quotes = [

    {
        quote: 'Javascript is a very tricky programming lanuage',
        source: 'Samantha Ollivierre', 
        year: '1986'
    },

    {
        quote: 'Python Developers are really well paid',
        source: 'Jovan Ollivierre', 
        year: '2016'
    },

    {
        quote: 'I am currently in the process of doing my first project and I am scared',
        source: 'Jamie Tashan', 
        year: '2015'
    },

    {
        quote: 'I will not cheat on this project! lol',
        source: 'Alice Musso', 
        year: '2011'
  }

  ];



function getRandomQuote () {

  var random = quotes[Math.floor(Math.random() * quotes.length)];
  return random;

  }

function printQuote () {


   print = getRandomQuote();
  // Stuck on this part :( 

 document.getElementById('quote-box').innerHTML;

}

2 Answers

Erik Nuber
Erik Nuber
20,629 Points
function printQuote () {

   print = getRandomQuote();


 document.getElementById('quote-box').innerHTML;

to get at an object you use dot notation so print now would hold the object returned by the getRandomQuote() function

to get each item, you would use

print.quote //this locates the quote

print.source //this locates the source

print.year //this locates the year

so I am not sure what the assignment is but you could do this

var statement = '<p>In ' + print.year + ' student ' + print.source;
statement += ' said ' + print.quote + '.</p>';

 document.getElementById('quote-box').innerHTML = statement;
Will Hunting
Will Hunting
13,726 Points

Thanks Erick I ended up resolving it using below. Each time I select the button on my page one of my properties listed are executed :)

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

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