-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
101 lines (88 loc) · 2.83 KB
/
script.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
let movies = [];
let shows = [];
let movieIndex = 0;
let showIndex = 0;
let shuffledMovies = [];
let shuffledShows = [];
let clickCount = 0; // Initialize clickCount
// Fetch JSON data
function loadData() {
fetch('Json/movies.json')
.then(response => response.json())
.then(data => {
movies = data;
initMovieList();
})
.catch(error => console.error('Error loading movies:', error));
fetch('Json/tv.json')
.then(response => response.json())
.then(data => {
shows = data;
initShowList();
})
.catch(error => console.error('Error loading shows:', error));
}
// Fisher-Yates shuffle to randomize arrays
function shuffle(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]];
}
return array;
}
// Initialize shuffled movie list
function initMovieList() {
shuffledMovies = shuffle(movies.slice());
}
// Initialize shuffled show list
function initShowList() {
shuffledShows = shuffle(shows.slice());
}
// Get the next movie from the shuffled list
function getNextMovie() {
if (movieIndex >= shuffledMovies.length) {
movieIndex = 0; // Reset index and shuffle again
shuffledMovies = shuffle(movies.slice());
}
return shuffledMovies[movieIndex++];
}
// Get the next show from the shuffled list
function getNextShow() {
if (showIndex >= shuffledShows.length) {
showIndex = 0; // Reset index and shuffle again
shuffledShows = shuffle(shows.slice());
}
return shuffledShows[showIndex++];
}
// Display media function
function displayRandomMedia(type) {
const contentContainer = document.querySelector('.content-container');
if (!contentContainer) {
console.error('Content container not found.');
return;
}
const content = contentContainer.querySelector('.content');
if (content) {
content.remove();
}
const newContent = document.createElement('div');
newContent.className = 'content'; // Add class for easier removal
if (type === 'movie') {
const randomMovie = getNextMovie();
newContent.innerHTML = `
<h2>${randomMovie.title}</h2>
<img src="${randomMovie.image}" alt="${randomMovie.title}" style="max-width: 40%;">
<p>${randomMovie.description}</p>
`;
} else if (type === 'show') {
const randomTVShow = getNextShow();
newContent.innerHTML = `
<h2>${randomTVShow.title}</h2>
<img src="${randomTVShow.image}" alt="${randomTVShow.title}" style="max-width: 40%;">
<p>${randomTVShow.description}</p>
`;
}
contentContainer.appendChild(newContent);
}
// Call loadData to fetch the JSON data
loadData();