Advertisement
differen71

Untitled

Nov 23rd, 2023
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movieInfo(dataArray) {
  2.     let moviesLibrary = [];
  3.  
  4.     function addMovie(movieName) {
  5.         let movieObject = { name: movieName };
  6.         moviesLibrary.push(movieObject);
  7.     }
  8.  
  9.     function addDirector(movie, director) {
  10.         let searchedMovie = moviesLibrary.find((movieObject) => movieObject.name === movie);
  11.         if (searchedMovie) {
  12.             searchedMovie['director'] = director;
  13.         };
  14.     }
  15.  
  16.     function addDate(movie, date) {
  17.         let searchedMovie = moviesLibrary.find((movieObject) => movieObject.name === movie);
  18.         if (searchedMovie) {
  19.             searchedMovie['date'] = date;
  20.         };
  21.     }
  22.  
  23.     dataArray.forEach(element => {
  24.         if (element.includes('addMovie')) {
  25.             let movieName = element.split('addMovie ')[1];
  26.             addMovie(movieName);
  27.  
  28.         } else if (element.includes('directedBy')) {
  29.             let [movieName, directorName] = element.split(" directedBy ");
  30.             addDirector(movieName, directorName);
  31.         }
  32.  
  33.         else if (element.includes('onDate')) {
  34.             let [movieName, movieDate] = element.split(" onDate ");
  35.             addDate(movieName, movieDate);
  36.         }
  37.     });
  38.  
  39.     for (const movie of moviesLibrary) {
  40.         if (movie.name && movie.director && movie.date)
  41.             console.log(JSON.stringify(movie))
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement