this is a code i made for a random quote generator and I want to make a similar one but with images this time. I want to put a resource in my page in which it’ll recommend you a book. You will have to click on a button and it will randomly show you a book cover.
For the quote generator I did this:
HTML:
<div class="quotes">
<h1 class="quote-generator">Simple Quote Generator</h1>
<div id="quoteDisplay"></div>
<button onclick="newQuote()" class="button-quote">New Quote</button>
<script src="./js/quotes.js"></script>
</div>
JavaScript
var quotes = [
'One must always be careful of books,<br>and what is inside them,<br>for words have the power to change us.<br>—Cassandra Clare',
'Only the very weak-minded refuse to be<br>influenced by literature and poetry.<br>—Cassandra Clare',
'I am not afraid of storms,<br>for I am learning how to sail my ship.<br>—Louisa May Alcott',
'Nice things don't happen in storybooks.<br>Or when they do happen, something bad happens next.<br>Because otherwise the story would be boring, and no one would read it.<br>—Holly Black',
'You don’t forget the face of the person who was your last hope.<br>—Suzanne Collins',
'You look best when you're you.<br>—Lynn Painter',
'“Everything’s a game, Avery Grambs.<br>The only thing we get to decide in this life<br>is if we play to win.<br>—Jennifer Lynn Barnes',
'“Why do I have to tell a story?” I asked.<br>“Because if you don’t tell the story,<br>someone else will tell it for you.”<br>—Jennifer Lynn Barnes',
'I mean, there’s a reason all books end right after the couple gets together.<br>No one wants to keep reading long enough to see the happily ever after turn into an unhappily ever after. Right?<br>—Alex Light',
'Break my heart,<br>break it a thousand times,<br>it was only ever yours to break anyway.<br>—Kiera Cass',
]
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
}
how can I change it the quotes that are in the variable quotes, to be images?
2
Answers
This can be achieved like below. I have slightly improved the logic to make sure that the same book is not offered twice.
Please let me know if this helps.
convert your array to Array of Objects. so you can also store images in object.
Please let me know if this works for you.