MartenBG

Movies

Nov 1st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. function solve(array) {
  2.  
  3. let movies = [];
  4.  
  5. array.forEach(el => {
  6. let line = el.split(' ');
  7.  
  8. if (line.includes('addMovie')) {
  9.  
  10. let movie = line.slice(1).join(' ');
  11.  
  12. movies.push({
  13. name: movie
  14. });
  15.  
  16. } else if (line.includes('directedBy')) {
  17.  
  18. let index = line.indexOf('directedBy');
  19. let movieName = line.slice(0, index).join(' ');
  20. let movieDirector = line.slice(index + 1).join(' ');
  21. let movie = movies.find(m => m.name === movieName);
  22.  
  23. if (movie) {
  24.  
  25. movie.director = movieDirector;
  26. }
  27.  
  28. } else if (line.includes('onDate')) {
  29.  
  30. let index = line.indexOf('onDate');
  31. let movieName = line.slice(0, index).join(' ');
  32. let movieDate = line.slice(index + 1).join(' ');
  33. let movie = movies.find(m => m.name === movieName);
  34.  
  35. if (movie) {
  36.  
  37. movie.date = movieDate;
  38. }
  39. }
  40. });
  41. console.log(movies);
  42.  
  43. for (const el of movies) {
  44.  
  45. let keys = Object.keys(el);
  46. if (keys.length === 3) {
  47.  
  48. console.log(JSON.stringify(el));
  49. }
  50. }
  51. }
Add Comment
Please, Sign In to add comment