Random Quote Generator Project review

Random Quote Generator Project review
0

#1

Hi there,
I’m working on a Random Code Generator project using HTML, CSS, and JavaScript. As part of the instructions, We are to Create a function named getRandomQuote which:
• selects a random quote object from the quotes array.
• returns the randomly selected quote object

I wanted to know if my function accurately does what the instruction says because I’m not so sure.
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() {
    var randomQuote = Math.floor(Math.random() * quotes.length);
return randomQuote;
}

#2

No, for two reaasons.

  1. You have not called the function to execute the code contained within it.

  2. You are assigning a number between 0 and the quotes array’s length - 1 to a variable named randomQuote. When you return randomQuote, it is not a quote, it is a number. I would renamed this variable to quoteIndex which is really what it represents. Then you can use this index to reference a quote object contained in the quotes array and assign this reference to a variable named randomQuote. Then your function would be returning a single random object from the quotes array.


#3
function getRandomQuote() {
var quoteIndex = Math.floor(Math.random() * quotes.length);
return quoteIndex;
}

Forgive me but I dont understand the referencing part, where I reference it to the quote object


#4

Your quotes variable is an array of elements. Those elements are objects. How do you normally reference an specific element of an array?

Answer: You use it’s index.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/access-array-data-with-indexes/


#5

Please what about this function?


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


#6

That function should return a random object from the quotes array if you call it in your code somewhere.


#7

Thanks for the feedback