To generate a random quote from a text file, you can use a random number generator to select a line from the file. By reading the text file line by line and storing each line in an array or list, you can then generate a random index within the range of the array's length. This index will correspond to a randomly chosen quote from the file. You can then display this quote to the user.
Pagination -
let currentPage = 1;
let pageSize = 12;
Change File Path -
const response = await fetch("Your_file.txt");
Function to display quotes for the current page -
async function displayQuotesForPage() {
const { quotesForPage, totalQuotes } = await fetchQuotesForPage(currentPage);
if (quotesForPage.length === 0 && currentPage > 1) {
// If no quotes are available and not on the first page, go back to page 1
currentPage = 1;
await displayQuotesForPage();
return;
}
Function to create a quote element -
quoteElement.innerHTML = `
<div class="card rounded-4">
<div class="card-body">
<p class="card-text font-monospace">"${quote}"</p>
</div>
</div>
`;
Function to display modal with the clicked quote -
function displayModal(quote) {
const modalBody = document.getElementById("quoteModalBody");
modalBody.textContent = quote;
const modal = new bootstrap.Modal(document.getElementById("quoteModal"));
modal.show();
}
Function to load the next page of quotes -
async function loadNextPage() {
currentPage++;
const { quotesForPage } = await fetchQuotesForPage(currentPage);
if (quotesForPage.length === 0) {
// Reset to page 1 if no quotes are available
currentPage = 1;
}
await displayQuotesForPage();
}