Advertisement
GalinaKG

Movies

Feb 23rd, 2023
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movies(input) {
  2.     let list = [];
  3.     for (const lines of input) {
  4.         if (lines.includes("addMovie")) {
  5.             let nameOfMovie = lines.split("addMovie ")[1];
  6.             list.push({ name:nameOfMovie })
  7.         } else if (lines.includes("directedBy")) {
  8.             let info = lines.split("directedBy ");
  9.             let name = info[0].trim();
  10.             let director = info[1];
  11.             let movie = list.find((movieObj) => movieObj.name === name)
  12.             if (movie) {
  13.                 movie.director = director;
  14.             }
  15.         } else if (lines.includes("onDate")) {
  16.             let info = lines.split("onDate ");
  17.             let name = info[0].trim();
  18.             let date = info[1];
  19.             let movie = list.find((movieObj) => movieObj.name === name);
  20.             if (movie) {
  21.                 movie.date = date;
  22.             }
  23.         }
  24.     }
  25.     for(const movie of list){
  26.         if(movie.name && movie.director && movie.date){
  27.             console.log(JSON.stringify(movie));
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement