didkoslawow

Untitled

Feb 26th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movies(moviesInput) {
  2.   class Movie {
  3.     constructor(movieName, director, date) {
  4.       this.name = movieName;
  5.     }
  6.   }
  7.  
  8.   const movieList = [];
  9.  
  10.   for (const movieInfo of moviesInput) {
  11.     if (movieInfo.includes('addMovie')) {
  12.       const movieName = movieInfo.replace('addMovie ', '');
  13.       const newMovie = new Movie(movieName);
  14.       movieList.push(newMovie);
  15.     } else if (movieInfo.includes('directedBy')) {
  16.       const movieInput = movieInfo.split(' ');
  17.       const index = movieInput.indexOf('directedBy');
  18.       const director = movieInput.splice(index).join(' ').replace('directedBy ', '');
  19.       const movieName = movieInput.join(' ');
  20.  
  21.       for (const movie of movieList) {
  22.         if (Object.values(movie).includes(movieName)) {
  23.           movie.director = director;
  24.         }
  25.       }
  26.     } else if (movieInfo.includes('onDate')) {
  27.       const movieInput = movieInfo.split(' ');
  28.       const index = movieInput.indexOf('onDate');
  29.       const date = movieInput.splice(index).join(' ').replace('onDate ', '');
  30.       const movieName = movieInput.join(' ');
  31.  
  32.       for (const movie of movieList) {
  33.         if (Object.values(movie).includes(movieName)) {
  34.           movie.date = date;
  35.         }
  36.       }
  37.     }
  38.   }
  39.  
  40.     for (const movie of movieList) {
  41.         if ('name' in movie && 'director' in movie && 'date' in movie) {
  42.             console.log(JSON.stringify(movie));
  43.         }
  44.     }
  45. }
Add Comment
Please, Sign In to add comment