Need help with Random quote machine


I’m about halfway through this project, but for whatever reason, on the render part of the JS section, it keeps saying it needs a semicolon, even though it wouldn’t make any sense. Can anyone help? Thanks!

  getRandomIndex = () => {
    const { quotes } = this.state;
    if(quotes.length > 0){
      const index = Math.floor(Math.random() * quotes.length);


In the previous function, you are missing a closing curly brace. That is one of the reasons to always practice good formatting.

1 Like

I ended up starting over, I’m trying to be mindful with curly brackets.
I’m mostly following a tutorial, though no buttons or quotes are showing. I feel like I forgot something obvious, just don’t know what.

I’ve made what I think is good progress since, though I still have a ways to go.

On Codepen, if you click the arrow drop-down in the JavaScript editor, there’s an option to format your code.

Yep! I just finished, and it actually passed all the tests…but the actual button doesn’t work lol. It passed all 12 tests and on FreeCodeCamp, but I still want to fix it if possible. I’m just not sure if I missed something or messed up somewhere.

If I finish this function, it works for me:

  const handleNewQuote = () => {};
1 Like

And this:

const getRandomIndex = (max) =>
  Math.round(Math.random() * (quoteData.length - 1 - 0) + 0);

How is this different than:

const getRandomIndex = () =>
  Math.round(Math.random() * (quoteData.length - 1 ));

And also, that is random but it isn’t an even distribution. Because you are rounding, the first index is only getting from 0 to .5, or 12.5% of the range (which you have as n-1). You have the same problem at the topSo, the distribution of the indexes would be:

0 - 12.52%
1 - 24.98%
2 - 24.99%
3 - 24.98%
4 - 12.53%

This is because you are trying to spread a range of 4 across 5 slots, and the first and last are only getting a half share, not what you want.

Traditionally, you get a random number for the whole range and then use the Math.floor. That way, each index will have have a whole number, 0 will have from 0 to .9999999999, 1 will have from 1 to 1.99999999, etc.

0 - 19.94%
1 - 19.96%
2 - 20.00%
3 - 20.08%
4 - 20.03%

You can mess around and see what I’m talking about like this:

  const NUM = 10000000
  const data = [0,0,0,0,0]
  for (let i = 0; i < NUM; i++) {
  data.forEach((datum, idx) => console.log(`${idx} - ${(100 * datum/NUM).toFixed(2)}%`))
1 Like

I changed the math.random portion, and I finished the function. It stilld does the same: passes all the tests but the button doesn’t actually produce any random quotes unless you refresh the page.
Thank you though!

const handleNewQuote = () => {};

Your handleNewQuote function does not do anything.