Advertisement
GeorgiLukanov87

04. Movies

Feb 23rd, 2023
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // NOT WORKING
  2.  
  3. function movie(input) {
  4.     let movieList = [];
  5.     let directorInfo = [];
  6.     let dateInfo = []
  7.  
  8.     let result = [];
  9.  
  10.     for (let details of input) {
  11.         if (details.includes('addMovie')) {
  12.             let addName = details.replace('addMovie ', '');
  13.             movieList.push(
  14.                 {
  15.                     'name': addName,
  16.                     'date': "",
  17.                     'director': "",
  18.                 }
  19.             )
  20.  
  21.         } else {
  22.             if (details.includes('directedBy')) {
  23.                 let splited_details = details.split(' directedBy ')
  24.                 let movieName = splited_details[0];
  25.                 let movieDirector = splited_details[1];
  26.                 directorInfo.push(movieName)
  27.                 directorInfo.push(movieDirector)
  28.  
  29.             }
  30.             else if (details.includes('onDate')) {
  31.                 let splited_details = details.split(' onDate ')
  32.                 let movieName = splited_details[0];
  33.                 let movieDate = splited_details[1];
  34.                 dateInfo.push(movieName)
  35.                 dateInfo.push(movieDate)
  36.             }
  37.         }
  38.  
  39.     }
  40.  
  41.     for (movie of movieList) {
  42.         for (let i = 0; i < dateInfo.length; i += 2) {
  43.             if (dateInfo[i] === movie.name) {
  44.                 movie.date = dateInfo[i + 1]
  45.             }
  46.         }
  47.  
  48.         for (let j = 0; j < directorInfo.length; j += 2) {
  49.             if (directorInfo[j] === movie.name) {
  50.                 movie.director = directorInfo[j + 1]
  51.             }
  52.         }
  53.  
  54.         if (movie.name !== '' && movie.date !== '' && movie.director !== '') {
  55.             result.push(`
  56.         {"name":"${movie.name}","director":"${movie.director}","date":"${movie.date}"}`
  57.             )
  58.         }
  59.  
  60.     }
  61.  
  62.     console.log(result.join('\n'))
  63.  
  64.  
  65. }
  66.  
  67.  
  68. // VS WORKING
  69.  
  70. function movieDirectors(array) {
  71.     let movies = [];
  72.  
  73.     class Movie {
  74.         constructor(name, director, date) {
  75.             this.name = name;
  76.             this.director = director;
  77.             this.date = date;
  78.         }
  79.     }
  80.     let contains = function (movieName) {
  81.         let movie = movies.find(m => m.name === movieName);
  82.         return movie;
  83.     }
  84.  
  85.     for (let i = 0; i < array.length; i++) {
  86.         let command = array[i].split(' ');
  87.  
  88.         if (command.includes('addMovie')) {
  89.             let name = command.slice(1, command.length).join(' ');
  90.             movies.push(new Movie(name, null, null));
  91.         } else if (command.includes('directedBy')) {
  92.             let name1 = command.slice(0, command.indexOf('directedBy')).join(' ');
  93.             if (contains(name1) !== undefined) {
  94.                 let movie = contains(name1);
  95.                 movie.director = command.slice(command.indexOf('directedBy') + 1, command.length).join(' ');
  96.             }
  97.         } else if (command.includes('onDate')) {
  98.             let name2 = command.slice(0, command.indexOf('onDate')).join(' ');
  99.             if (contains(name2) !== undefined) {
  100.                 let movie = contains(name2);
  101.                 movie.date = command.slice(command.indexOf('onDate') + 1, command.length).join(' ');
  102.             }
  103.         }
  104.     }
  105.     movies.forEach(m => m.director != null && m.name != null && m.date != null ? console.log(JSON.stringify(m)) : null);
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement