Skip to content
the poor coder

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

Poor Man
Published
Reading time
2 min read
On this page

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:

Text
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:

Text
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:

Text
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:

Text
<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:

Text
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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC