-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (52 loc) · 2.02 KB
/
script.js
File metadata and controls
65 lines (52 loc) · 2.02 KB
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
const API_KEY = '0f403c7e87bb9285f919447a42f8d565';
const API_URL = `https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=${API_KEY}`;
const searchInput = document.getElementById('searchInput');
const movieList = document.getElementById('movieList');
let movies = [];
function fetchMovies(url) {
fetch(url)
.then(response => response.json())
.then(data => {
movies = data.results;
displayMovies(movies);
})
.catch(error => {
console.error('Error:', error);
});
}// this function will fetch data from the website
function displayMovies(movies) {
movieList.innerHTML = '';
if (movies.length === 0) {
const noResultsMessage = document.createElement('p');
noResultsMessage.textContent = 'No movies found.';
movieList.appendChild(noResultsMessage);
return;
}
movies.forEach(movie => {
const movieElement = createMovieElement(movie);
movieList.appendChild(movieElement);
});
}//this function will display the fetched data on the screen
function createMovieElement(movie) {
const movieElement = document.createElement('div');
movieElement.classList.add('movie');
const img = document.createElement('img');
img.src = `https://image.tmdb.org/t/p/w500${movie.poster_path}`;
img.alt = movie.title;
movieElement.appendChild(img);
const h2 = document.createElement('h2');
h2.textContent = movie.title;
movieElement.appendChild(h2);
const p = document.createElement('p');
p.textContent = movie.overview;
movieElement.appendChild(p);
return movieElement;
}//this function will create every movie into an HTML element to make the search functionality
searchInput.addEventListener('input', (event) => {
const searchTerm = event.target.value.toLowerCase();
const filteredMovies = movies.filter(movie =>
movie.title.toLowerCase().includes(searchTerm)
);
displayMovies(filteredMovies);
});//this will enable the search functionality
fetchMovies(API_URL); //fecth API command