I am working on a project where I have a collection of books embedded as iframe elements. My goal is to select three random books from this list and display them within a pre-defined div
. However, despite my attempts to use the shuffle method, I am encountering issues with displaying or randomizing the elements.
Below is an example of the code snippet I am currently working with:
Javascript
function getRandomBooks(arr, count) {
var arr = [ // List of embedded book iframes
'<iframe type="text/html" width="150" height="200" frameborder="0" allowfullscreen style="max-width:100%" src="https://read.amazon.com/kp/card?asin=B00ARFNQ54&asin=B00ARFNQ54&preview=newtab&linkCode=kpe&ref_=cm_sw_r_kb_dp_wpdGxbH9ZAXX9&hideShare=true" ></iframe>',
... (remaining array entries omitted for brevity)
];
var insertDiv = document.getElementsByClassName("bookFrame"); // Target div for inserting elements
var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
var selectedBooks = shuffled.slice(min, 3);
insertDiv.innerHTML(selectedBooks); // Insert 3 randomly selected books into the designated div
alert(getRandomBooks(arr, 3)); // Test call
};
HTML
<p class="center bookFrame">
<!-- Random books will be inserted here -->
</p>