Skip to content Skip to sidebar Skip to footer

Javascript - Random Quote Generator That Does Not Repeat Code

This is my very first post on Stack overflow. I've tried several examples here on how to overcome this but cannot find a a fix.(I'm still a newbie with javascript). I have create

Solution 1:

One way is to change your getRandomQuote() function a bit:

functiongetRandomQuote() {
  return quotes.splice(Math.floor(Math.random() * quotes.length), 1);
}

Explanation: Array.splice() removes items from an array, and returns the removed items. First argument is where to start splicing, and the second argument is how many items to remove. This way the used quotes are removed when used, and so can't be used again until the page is reloaded.

Solution 2:

I like your idea and really reading those quotes! Perhaps try editing:

document.getElementById('loadQuote').addEventListener("click", printQuote, true);

Might be a bit outdated. Try:

document.getElementById('loadQuote').onclick = function(name){yourScript}; 

Suggesting this because I'm getting the following error message when trying out your code:

"message": "Uncaught TypeError: Cannot read property 'addEventListener' of null". Just try simplifying.

I think your work is very impressive for someone who considers themselves a beginner!

Post a Comment for "Javascript - Random Quote Generator That Does Not Repeat Code"