Only passing #9 about every other time

I am stumped. It seems to go back and forth from passing #9 to failing, this also happened with #8.

error:
Timeout of 15000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves.
Error: Timeout of 15000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves.
at i.g._timeoutError (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:39286)
at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:37376

Right, it is hitting the new quote button and checking if there is a new quote, as in different than the previous quote. So, if your random quote is #1, then the random index happens to be 1 the next time, it will fail that test. Considering that you have three quotes, I would expect that to happen about 1/3 of the time.

So, I see two options:

  • The “easy” way - Add more quotes. The more quotes you have, the less likely it is to get two of the same in a row. This is kind of cheating, but OK.
  • The “better way” - See if you can find a way to ensure that you don’t get the same index twice in a row. I can think of two ways - an ugly way and an elegant way.

I didn’t realize it had to be a different quote than the last. Would one of the options be using an if statement?

Well, I guess I can elaborate…

The common method with this would be something like a while loop, You loop until the new index is not equal to the old sequence. An if statement wouldn’t work since you may have to do it more than once. This is the method that probably first occurs to people, but I think it’s messy.

I think a more elegant approach would be to consider the possible values needed in a circle. If there are 10 possible indices, then the next indices should be 1-9 spaces away. I could generate that random number, then add it to my index and take the modulus of the total number of possible indices. Then I have a new index without having to loop indefinitely.

Thanks for explaining this to me. I was able to make it work with a while loop, but still working on trying to make the elegant approach work.

edit: Not working actually.

If you get stumped, I recently discussed this more extensively here.

1 Like

Thanks, It works now.