-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtensorscript.js
69 lines (55 loc) · 2.08 KB
/
tensorscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Function to generate random integer within a range
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Function to shuffle an array in place
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// Function to handle the "Draw Cards" button click event
function drawCards() {
const numCards = parseInt(document.getElementById('num-cards').value);
const cardContainer = document.getElementById('card-container');
// Clear the card container
cardContainer.innerHTML = '';
// Retrieve the card data from the JSON
fetch('tensordb.json')
.then(response => response.json())
.then(data => {
// Shuffle the card data array
shuffleArray(data);
// Draw the specified number of cards
for (let i = 0; i < numCards; i++) {
const card = data[i];
const cardElement = createCardElement(card);
cardContainer.appendChild(cardElement);
}
})
.catch(error => console.error('Error:', error));
}
// Function to create a card element
function createCardElement(card) {
const cardElement = document.createElement('div');
cardElement.className = 'card';
const cardImage = document.createElement('img');
cardImage.className = 'card-image';
cardImage.src = card.cardpic;
cardImage.alt = card.cardname;
const cardInfo = document.createElement('div');
cardInfo.className = 'card-info';
const cardName = document.createElement('h2');
cardName.textContent = card.cardname;
const cardDesc = document.createElement('p');
cardDesc.textContent = card.carddesc;
cardInfo.appendChild(cardName);
cardInfo.appendChild(cardDesc);
cardElement.appendChild(cardImage);
cardElement.appendChild(cardInfo);
return cardElement;
}
// Event listener for the "Draw Cards" button
const drawCardsButton = document.getElementById('draw-cards-button');
drawCardsButton.addEventListener('click', drawCards);