Rendom Quotes With .Push() And .indexof() Method

Random Quotes with .push() and .indexOf() Method

Have you ever wanted to randomly display a quote on your website or blog? If so, you can use the .push() and .indexOf() method in JavaScript to accomplish this task.

Step 1: Create an Array of Quotes

First, create an array of quotes. Each quote should be a string enclosed in quotation marks and separated by commas. For example:

var quotes = [
  "The best way to predict the future is to create it. -Peter Drucker",
  "Success is not final, failure is not fatal: It is the courage to continue that counts. -Winston Churchill",
  "Quality is not an act, it is a habit. -Aristotle",
  "Believe you can and you're halfway there. -Theodore Roosevelt",
  "Challenges are what make life interesting and overcoming them is what makes life meaningful. -Joshua J. Marine",
  "Don't watch the clock; do what it does. Keep going. -Sam Levenson"
];

Step 2: Generate a Random Quote

Next, use the Math.random() method to generate a random number between 0 and the length of the quotes array minus one. Then, use the .push() method to add the randomly selected quote to a new array called randomQuotes. For example:

var randomQuotes = [];

function generateRandomQuote() {
  var randomNumber = Math.floor(Math.random() * quotes.length);
  randomQuotes.push(quotes[randomNumber]);
}

Step 3: Display the Random Quote

Finally, use the .indexOf() method to find the index of the last item in the randomQuotes array, which will be the most recently added quote. Then, display this quote on your website or blog. For example:

function displayRandomQuote() {
  var lastQuoteIndex = randomQuotes.length - 1;
  document.getElementById("quote").innerHTML = randomQuotes[lastQuoteIndex];
}

To display the quote on your website, simply add an HTML element with an id of "quote" where you want the quote to appear:

<div id="quote"></div>

Now, every time the generateRandomQuote() function is called, a new quote will be added to the randomQuotes array and displayed on your website.

Alternative Method: Use splice() method

Another way to generate a random quote is to use the .splice() method instead of .push(). This method removes and returns a random quote from the quotes array, which can then be displayed on your website. For example:

function generateRandomQuote() {
  var randomNumber = Math.floor(Math.random() * quotes.length);
  var randomQuote = quotes.splice(randomNumber, 1)[0];
  randomQuotes.push(randomQuote);
}

This method has the advantage of not repeating a quote until all quotes have been displayed, as each quote is removed from the quotes array once it has been selected.

Conclusion

Using the .push() and .indexOf() method or .splice() method in JavaScript is a simple way to generate and display random quotes on your website or blog. With a little bit of code, you can add some extra inspiration or humor to your online presence.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe