Todorov_Stanimir

04. Movies Objects and Classes - Exercise Last decision

Jun 12th, 2019
99
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. }
Add Comment
Please, Sign In to add comment