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

Creating the getRandomQuote Function in the Random Quote Generator Project

I'm working on a Random Quote Generator Project, and as part of the project, the second instruction is to create a getRandomQuote Function which * should take in one parameter that will later get passed into this function when it is invoked will be the array of quotes. * The body of the function should select and return a random quote object from the quotesarray.

I'm kinda confused with my code, I want a review on my code as to whether its fits the instruction. Below is my code, thanks!!

// An array of Objects with the quote and source as properties var quotes = [ { quote: "In the end, it’s not the years in your life that count. It’s the life in your years", source: "Abraham Lincoln",

},
{
    quote: "Don’t gain the world and lose your soul, wisdom is better than silver or gold",
    source: "Bob Marley",

},
{
    quote: "Lighten up, just enjoy life, smile more, laugh more, and don’t get so worked up about things",
    source: "Kenneth Branagh",

},
{
    quote: "Don’t cry because it’s over, smile because it happened",
    source: "Ludwig Jacobowski",

},
{
    quote: "Do stuff. Be clenched, curious. Not waiting for inspiration’s shove or society’s kiss on your forehead. Pay attention. It’s all about paying attention. Attention is vitality. It connects you with others. It makes you eager. Stay eager",
    source: "Susan Sontag",

}

];

// getRandomQuote function selects and returns a random quote object function getRandomQuote(array) { for (var i = 0; i < quotes.length; i++) { var quoteIndex = Math.floor(Math.random() * quotes.length); var randomQuote = quoteIndex; } return randomQuote; }

5 Answers

Hey Steve

Not far off. Your random quote function only created a random number. You needed to assign that random number to an array index to get a different result each time.

var quotes = [{
    quote: "Don’t gain the world and lose your soul, wisdom is better than silver or gold",
    source: "Bob Marley",

},
{
    quote: "Lighten up, just enjoy life, smile more, laugh more, and don’t get so worked up about things",
    source: "Kenneth Branagh",

},
{
    quote: "Don’t cry because it’s over, smile because it happened",
    source: "Ludwig Jacobowski",

},
{
    quote: "Do stuff. Be clenched, curious. Not waiting for inspiration’s shove or society’s kiss on your forehead. Pay attention. It’s all about paying attention. Attention is vitality. It connects you with others. It makes you eager. Stay eager",
    source: "Susan Sontag",

}];

function getRandomQuote(array) { 
  // Random number generator
  var quoteIndex = Math.floor(Math.random() * quotes.length); 

  for (var i = 0; i < array.length; i++) { 
// array.length rather than the actual quotes variable makes this function a little bit more flexible

    var randomQuote = array[quoteIndex]; 
    // Random quote variable with the index set to your random number variable 
  } 
  return randomQuote; // Returns random quote variable 
}
// Passes quotes array as an argument and stores result of function in variable
var result = getRandomQuote(quotes); 


console.log(result);

If i haven't formatted/explained this well, give me a shout.

Happy coding

Paul

It makes sense now. Thanks.

But what about this code below ?

function getRandomQuote() { var quoteIndex = Math.floor(Math.random() * quotes.length); // selects a random quote return quotes[quoteindex]; // returns the random quote }

The variable quoteIndex is assigned a number between 0-3 as there are 4 items in the array. It's essentially a random number generator. You then use this number as an index to pull a specific quote from the quotes array.

I'm on the third part of the project, using the printQuote function and below is my code but after everything, I get an undefined.

/printQuote function
function printQuote() {
    var result = getRandomQuote(); // Calls and stores the getRandomQuote in a variable
    var message = "<p class='quote'>" + quotes.quote + "</p>" +"<p class='source'>" + quotes.source + "</p>";
    document.getElementById('quote-box').innerHTML = message;
}



// event listener to respond to "Show another quote" button clicks
// when user clicks anywhere on the button, the "printQuote" function is called
document.getElementById('loadQuote').addEventListener("click", printQuote, false);

Below is the getRandomCode function I used

// getRandomQuote function selects and returns a random quote object
function getRandomQuote() {
    var quoteIndex = Math.floor(Math.random() * quotes.length);  // selects a random quote
    return quotes[quoteIndex];  // returns the random quote
}

In your getRandomQuote function you need to pass the quotes array as an argument then loop over the array using a for loop.

function getRandomQuote(array) {
  var quoteIndex = Math.floor(Math.random() * quotes.length);  
  for (var i = 0; i < array.length; i++) {
    var randomQuote = array[quoteIndex];
  } 
  return randomQuote;
}

Okay, thanks for the feedback!