audipower

Movies - Object and Classes

Jun 12th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movies(commands) {
  2.     let movies = {};
  3.     let result = [];
  4.     for (let i = 0; i < commands.length; i++) {
  5.         let operation = String(commands[i]);
  6.         if (operation.includes('addMovie')) {
  7.             let name = operation.substr(9)
  8.             if (!movies.hasOwnProperty(name)) {
  9.                 movies[name] = {};
  10.                 movies[name].name = name;
  11.             }
  12.         } else if (operation.includes(' directedBy ')) {
  13.             let [name, director] = operation.split(' directedBy ');
  14.             if (movies.hasOwnProperty(name)) {
  15.                 movies[name].director = director;
  16.             }
  17.         } else if (operation.includes(' onDate ')) {
  18.             let [name, date] = operation.split(' onDate ');
  19.             if (movies.hasOwnProperty(name)) {
  20.                 movies[name].date = date;
  21.             }
  22.         }
  23.     }
  24.     for (name of Object.keys(movies)) {
  25.         if (movies[name].director !== undefined && movies[name].date !== undefined) {
  26.             result.push(movies[name])
  27.         }
  28.     }
  29.     result.forEach(film => console.log(JSON.stringify(film)));
  30. }
  31.  
  32. movies([
  33.     'addMovie Fast and Furious',
  34.     'addMovie Godfather',
  35.     'Inception directedBy Christopher Nolan',
  36.     'Godfather directedBy Francis Ford Coppola',
  37.     'Godfather onDate 29.07.2018',
  38.     'Fast and Furious onDate 30.07.2018',
  39.     'Batman onDate 01.08.2018',
  40.     'Fast and Furious directedBy Rob Cohen'
  41. ]);
Add Comment
Please, Sign In to add comment