TypeScript - Movies - Interface, Objects, For Loop #146
Replies: 13 comments
-
|
interface Movie{ let movieList = [ for(let key in movieList){ } function pushFunction() { } |
Beta Was this translation helpful? Give feedback.
-
Codeinterface Movie {
title: string;
director: string;
releaseYear: number;
rating: number;
}
let movieList: Movie[] = [
{
title: "The Matrix",
director: "The Wachowskis",
releaseYear: 1999,
rating: 9,
},
{
title: "Inception",
director: "Christopher Nolan",
releaseYear: 2010,
rating: 8,
},
{
title: "The Shawshank Redemption",
director: "Frank Darabont",
releaseYear: 1994,
rating: 10,
}
];
for (let movie in movieList) {
console.log(movieList[movie].title, ':', movieList[movie].rating);
}
let addMovie = function(movie: Movie) {
movieList.push(movie);
return movieList;
}
let deleteMovie = function(title: string) {
return movieList.filter(movie => {
return title !== movie['title']
});
}
let getMovieByDirector = function(director: string) {
return movieList.filter(movie => {
return director === movie['director']
});
}Output |
Beta Was this translation helpful? Give feedback.
-
|
interface Movie{ let movieList = [ for(let key in movieList){ } function pushFunction() { } |
Beta Was this translation helpful? Give feedback.
-
|
Code |
Beta Was this translation helpful? Give feedback.
-
|
interface Movie_vikas { let movieList: Movie_vikas[] = [ for(let key in movieList){ function addMovie(x: Movie_vikas): any { } addMovie({ for(let key in movieList){ |
Beta Was this translation helpful? Give feedback.
-
interface Movie {
title: string;
director: string;
releaseYear: number;
rating: number;
}
const movieList: Movie[] = [
{
title: "The Matrix",
director: "The Wachowskis",
releaseYear: 1999,
rating: 9,
},
{
title: "Inception",
director: "Christopher Nolan",
releaseYear: 2010,
rating: 8,
},
{
title: "The Shawshank Redemption",
director: "Frank Darabont",
releaseYear: 1994,
rating: 10,
},
];
for (const movie of movieList) {
console.log(`Title: ${movie.title}, Rating: ${movie.rating}`);
}
function addMovie(movie: Movie): void {
movieList.push(movie);
}
function deleteMovie(title: string): void {
const index = movieList.((movie) => movie.title === title);
if (index !== -1) {
movieList.splice(index, 1);
}
}
function getMovieByDirector(director: string): Movie[] {
return movieList.filter((movie) => movie.director === director);
}
const newMovie: Movie = {
title: "The Dark Knight",
director: "Christopher Nolan",
releaseYear: 2008,
rating: 9,
};
addMovie(newMovie);
console.log(movieList); // Output: updated movieList array with new movie
deleteMovie("Inception");
console.log(movieList); // Output: updated movieList array without Inception
const moviesByNolan = getMovieByDirector("Christopher Nolan");
console.log(moviesByNolan); // Output: array of movies directed by Christopher Nolan |
Beta Was this translation helpful? Give feedback.
-
|
interface Movie { const movieList: Movie[] = [ for (const movie of movieList) { function addMovie(movie: Movie): void { function deleteMovie(title: string): void { function getMovieByDirector(director: string): Movie[] { |
Beta Was this translation helpful? Give feedback.
-
Codeinterface Movie {
title: string,
director: string,
releaseYear: number,
rating: number
}
let movieList: Movie[] = [];
movieList.push({title: 'The Matrix', director: 'The Wachwskis', releaseYear: 1999, rating:9});
//add movie using function addMovie()
addMovie({title: 'Inception', director: 'Christopher Nolan', releaseYear: 2010, rating:8});
addMovie({title: 'The Shawshank Redemption', director: 'Frank Darabont', releaseYear: 1994, rating:10});
addMovie({title: 'The Prestige', director: 'Christopher Nolan', releaseYear: 2006, rating:7});
addMovie({title: 'Following', director: 'Christopher Nolan', releaseYear: 1998, rating:7.1});
//display title and rating
for (let movie in movieList) {
console.log("Title : " + movieList[movie].title)
console.log("Rating : " + movieList[movie].rating);
}
//delete movie using function deleteMovie()
deleteMovie('The Prestige');
//get movie list be director
let movieListByDirector = getMovieByDirector('Christopher Nolan');
console.log("Movies directed by Christopher Nolan: " + movieListByDirector);
//function to add movie
function addMovie(movie: Movie) {
movieList.push(movie);
return;
}
function deleteMovie(title: string) {
for (let movie in movieList) {
if (movieList[movie].title == title) {
delete movieList[movie];
console.log ("Deleted the movie: " + title);
}
}
return;
}
function getMovieByDirector(director: string): string[] {
let moviesListDirector: string[] = []
for (let movie in movieList) {
if (movieList[movie].director == director) {
moviesListDirector.push(movieList[movie].title);
}
}
return moviesListDirector;
}Screenshot |
Beta Was this translation helpful? Give feedback.
-
|
interface Movie { const movieList: Movie[] = [ function addMovie(movie: Movie): void { function deleteMovie(title: string): void { function getMovieByDirector(director: string): Movie[] { for (const movie of movieList) { |
Beta Was this translation helpful? Give feedback.
-
Codeinterface Movie {
title: string;
director: string;
releaseYear: number;
rating: number;
}
const movieList: Movie[] = [
{
title: "The Matrix",
director: "The Wachowskis",
releaseYear: 1999,
rating: 9,
},
{
title: "Inception",
director: "Christopher Nolan",
releaseYear: 2010,
rating: 8,
},
{
title: "The Shawshank Redemption",
director: "Frank Darabont",
releaseYear: 1994,
rating: 10,
},
];
for (const movie of movieList) {
console.log(`Title: ${movie.title}, Rating: ${movie.rating}`);
}
//Add New Movie
function addMovie(movie: Movie): void {
movieList.push(movie);
}
const newMovie: Movie = {
title: "The Dark Knight",
director: "Christopher Nolan",
releaseYear: 2008,
rating: 9,
};
addMovie(newMovie);
console.log(movieList);
//Delete Movie
function deleteMovie(title: string): void {
let index = -1;
for (let i = 0; i < movieList.length; i++) {
if (movieList[i].title === title) {
index = i;
break;
}
}
if (index !== -1) {
movieList.splice(index, 1);
}
}
deleteMovie("Inception");
console.log(movieList);
//Get Movie by Directory
function getMovieByDirector(director: string): Movie[] {
return movieList.filter((movie) => movie.director === director);
}
const moviesByNolan = getMovieByDirector("The Wachowskis");
console.log(moviesByNolan); ScreenShot |
Beta Was this translation helpful? Give feedback.
-
Codeinterface movie {
Title : String;
director : String;
releaseYear: number;
rating : number;
}
let movieList : movie[] = [
{
Title: "The Matrix",
director: "The Wachowskis",
releaseYear: 1998,
rating: 9,
},
{
Title: "Inception",
director: "Christopher Nolan",
releaseYear: 2010,
rating: 8,
},
{
Title: "The Shawshank Redemption",
director: "Frank Darabont",
releaseYear: 1994,
rating: 10,
},
];
console.log(movieList);
for (let key in movieList) {
console.log(`title : ${movieList[key].Title}, rating : ${movieList[key].rating}`);
console.log("Title : " + movieList[key].Title + ", Rating : " + movieList[key].rating);
}
const newMovie : movie = {
Title: "Lost in Space",
director: "Christan Lopise",
releaseYear: 2021,
rating: 9,
}
function addMovie(Movie : movie) {
movieList.push(Movie);
console.log("New movie added at list : " , movieList);
}
console.log(newMovie);
addMovie(newMovie);
function deleteMovie(movieName : string) {
for (let movie in movieList) {
if (movieName == movieList[movie].Title) {
delete movieList[movie];
console.log("This",movieName, "deleted from database : ", movieList);
} else {
console.log(`Movie not found: ${movieName}`);
}
}
}
deleteMovie("Inception");
function getMovieByDirector(directorName : string) {
for(let director in movieList) {
if(movieList[director].director == directorName) {
console.log("This director is directed" , directorName, movieList[director] );
} else {
console.log("the director is not found");
}
}
}
getMovieByDirector("The Wachowskis");output |
Beta Was this translation helpful? Give feedback.
-
CODEinterface Movie {
title: string;
director: string;
releaseYear: number;
rating: number;
}
const movieList: Movie[] = [
{ title: "The Matrix", director: "The Wachowskis", releaseYear: 1999, rating: 9 },
{ title: "Inception", director: "Christopher Nolan", releaseYear: 2010, rating: 8 },
{ title: "The Shawshank Redemption", director: "Frank Darabont", releaseYear: 1994, rating: 10 }
];
// Output title and rating of each movie in movieList
for (const movie of movieList) {
console.log(`Title: ${movie.title}, Rating: ${movie.rating}`);
}
function addMovie(movie: Movie): void {
movieList.push(movie);
}
function deleteMovie(title: string): void {
const index = movieList.findIndex(movie => movie.title === title);
if (index !== -1) {
movieList.splice(index, 1);
}
}
function getMovieByDirector(director: string): Movie[] {
return movieList.filter(movie => movie.director === director);
}
SCREENSHORT |
Beta Was this translation helpful? Give feedback.
-
Codeinterface Movie{
title: string;
director: string;
releaseYear: number;
rating: number;
}
let movieList = [] = [{
title: 'The Matrix',
director: 'The Wachowskis',
releaseYear: 1999,
rating: 9
},
{ title: 'Inception',
director: 'Christopher Nolan',
releaseYear: 2010,
rating: 8
},
{
title: 'The Shawshank Redemption',
director: 'Frank Darabont',
releaseYear: 1994,
rating: 10
},
{
title: 'Bound',
director: 'The Wachowskis',
releaseYear: 1996,
rating: 7.3
}
]
for(let i = 0; i < movieList.length; i++){
console.log(`the title and rating of each movie are ${movieList[i].title} & ${movieList[i].rating}`);
}
function addMovie(movie: Movie) {
movieList.push(movie);
return true;
}
function deleteMovie(movietitle: string) {
for(let i = 0; i < movieList.length; i++) {
if(movietitle == movieList[i].title){
movieList.splice(i, 1);
}
}
// const index = movieList.findIndex((movie) => movie.title === movietitle);
//if (index !== -1) {
//movieList.splice(index, 1);
}
const directorlist: string[] = [];
function getMovieByDirector(movieDirectorName: string) {
for(let i = 0; i < movieList.length; i++) {
if(movieDirectorName == movieList[i].director){
directorlist.push(movieList[i].title);
}
}
// const index = movieList.findIndex((movie) => movie.title === movietitle);
//if (index !== -1) {
//movieList.splice(index, 1);
}
let newMovie = { title: "The Dark Knight",
director: "Christopher Nolan",
releaseYear: 2008,
rating: 9,}
addMovie(newMovie);
console.log('-------------------------------- List of movies after addition');
console.log(movieList);
deleteMovie("Inception")
console.log('-------------------------------- List of movies after deletion');
console.log(movieList);
console.log('--------------------------------')
getMovieByDirector("The Wachowskis");
console.log(directorlist);Output |
Beta Was this translation helpful? Give feedback.








Uh oh!
There was an error while loading. Please reload this page.
-
Task Description
You are tasked with creating a program that stores information about a list of movies in an array of objects. Your program should allow the user to add, access, and delete information about the movies.
Requirements
Create an interface called Movie with the following properties:
titleof typestringdirectorof typestringreleaseYearof typenumberratingof typenumberCreate an empty array called movieList that will store all the movies.
Use the Movie interface to create an object for each movie in the movieList array. The movies in the array should be:
Use a for loop to iterate through the movieList array and output the title and rating of each movie using the console.log() function.
Create a function called addMovie() that accepts a Movie object as a parameter and adds it to the movieList array.
Create a function called deleteMovie() that accepts a string parameter representing the title of the movie to delete. The function should remove the movie from the movieList array.
Create a function called getMovieByDirector() that accepts a string parameter representing the name of the director. The function should iterate through the movieList array and return an array of all the movies with the specified director.
You should use TypeScript to define the types of the input and output parameters for the functions.
Note
addMovie(),deleteMovie(), andgetMovieByDirector(). TheaddMovie()function accepts aMovieobject as a parameter and adds it to themovieListarray. ThedeleteMovie()function accepts astringparameter representing the title of the movie to delete and removes it from themovieListarray. ThegetMovieByDirector()function accepts astringparameter representing the name of the director and returns an array of all the movies with the specified director.Beta Was this translation helpful? Give feedback.
All reactions