Advertisement
Grossos

04-movies.js

Oct 28th, 2023
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Movies
  2. // Write a function that stores information about movies inside an array. The movie's object info must be name,
  3. // director, and date. You can receive several types of input:
  4. // • "addMovie {movie name}" – add the movie
  5. // • "{movie name} directedBy {director}" – check if the movie exists and then add the director
  6. // • "{movie name} onDate {date}" – check if the movie exists and then add the date
  7. // At the end print all the movies that have all the info (if the movie has no director, name, or date, don’t print it) in
  8. // JSON format.
  9.  
  10. function manageMovies(arr) {
  11.  
  12.     let movies = [];
  13.  
  14.     for (let elem of arr) {
  15.  
  16.         if (elem.includes('addMovie')) {
  17.             let movieName = elem.split('addMovie ')[1];
  18.             let movieObj = { name: movieName }
  19.             movies.push(movieObj);
  20.  
  21.         } else if (elem.includes('directedBy')) {
  22.             let [movieName, directorName] = elem.split(' directedBy ');
  23.  
  24.             let movie = movies.find(anyMovie => anyMovie.name == movieName); // текущия обект за филм ?!?!
  25.  
  26.             if (movie) {
  27.                 movie.director = directorName;
  28.          
  29.             }
  30.  
  31.         } else if (elem.includes('onDate')) {
  32.             let [movieName, date] = elem.split(' onDate ');
  33.  
  34.             let movie = movies.find(anyMovie => anyMovie.name == movieName); // текущия обект за филм ?!?!
  35.  
  36.             if (movie) {
  37.                 movie.date = date;
  38.             }
  39.  
  40.         }
  41.     }
  42.    
  43.     console.log(Object.entries(movies));
  44.  
  45. }
  46.  
  47.  
  48. manageMovies([
  49.     'addMovie Fast and Furious',
  50.     'addMovie Godfather',
  51.     'Inception directedBy Christopher Nolan',
  52.     'Godfather directedBy Francis Ford Coppola',
  53.     'Godfather onDate 29.07.2018',
  54.     'Fast and Furious onDate 30.07.2018',
  55.     'Batman onDate 01.08.2018',
  56.     'Fast and Furious directedBy Rob Cohen'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement