-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (70 loc) · 2.99 KB
/
script.js
File metadata and controls
82 lines (70 loc) · 2.99 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Global movies array
const movies = [
{ title: "Inception", genre: "Sci-Fi", rating: 8.8, releaseYear: 2010 },
{ title: "The Dark Knight", genre: "Action", rating: 9.0, releaseYear: 2008 },
{ title: "Interstellar", genre: "Sci-Fi", rating: 8.6, releaseYear: 2014 }
];
// Function to add a movie
function addMovies() {
const title = document.getElementById('title').value.trim();
const genre = document.getElementById('genre').value.trim();
const rating = parseFloat(document.getElementById('rating').value);
const releaseYear = parseInt(document.getElementById('releaseYear').value);
if (!title || !genre || isNaN(rating) || isNaN(releaseYear)) {
alert('Please enter valid movie details.');
return;
}
const newMovie = { title, genre, rating, releaseYear };
movies.push(newMovie);
alert('Movie added successfully!');
}
// Function to list movies by genre
function listMoviesByGenre() {
const genre = document.getElementById('genreInput').value.trim();
if (!genre) {
alert('Please enter a genre.');
return;
}
const filteredMovies = movies.filter(movie => movie.genre.toLowerCase() === genre.toLowerCase());
const outputDiv = document.getElementById('genreOutput');
outputDiv.innerHTML = filteredMovies.length > 0
? filteredMovies.map(movie => `${movie.title} (${movie.releaseYear}) - Rating: ${movie.rating}`).join('<br>')
: 'No movies found in this genre.';
}
// Function to find the highest-rated movie
function findHighestRatedMovie() {
if (movies.length === 0) {
alert('No movies in the collection.');
return;
}
const highestRated = movies.reduce((highest, movie) => movie.rating > highest.rating ? movie : highest, movies[0]);
const outputDiv = document.getElementById('highestRatedOutput');
outputDiv.innerText = `Highest Rated Movie: ${highestRated.title} (${highestRated.releaseYear}) - Rating: ${highestRated.rating}`;
}
// Function to get movie titles
function getMovieTitles() {
if (movies.length === 0) {
alert('No movies in the collection.');
return;
}
const titles = movies.map(movie => movie.title);
const outputDiv = document.getElementById('titlesOutput');
outputDiv.innerText = `Movies: ${titles.join(', ')}`;
}
// Function to list movies after a certain year
function moviesAfterYear() {
const year = parseInt(document.getElementById('yearInput').value);
if (isNaN(year)) {
alert('Please enter a valid year.');
return;
}
const filteredMovies = movies.filter(movie => movie.releaseYear > year);
const outputDiv = document.getElementById('afterYearOutput');
outputDiv.innerHTML = filteredMovies.length > 0
? filteredMovies.map(movie => `${movie.title} (${movie.releaseYear}) - Rating: ${movie.rating}`).join('<br>')
: 'No movies found after this year.';
}
// Function to clear the displayed output
function clearOutput(sectionId) {
document.getElementById(sectionId).innerText = '';
}