Ivankooo1

movies

Nov 3rd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movie(arr){
  2.   let movies = [];
  3.   for(let command of arr){
  4.     let tokens = command.split(' ');
  5.    
  6.     if(tokens[0] === 'addMovie'){
  7.       let filmName = tokens.slice(1).join(' ');
  8.       movies.push({name: filmName});
  9.     }else if(tokens.includes('directedBy')){
  10.       let directedByIndex = tokens.indexOf('directedBy');
  11.       let filmName = tokens.slice(0,directedByIndex).join(' ');
  12.  
  13.       let film = movies.find((o) => o.name === filmName);
  14.  
  15.       if(film){
  16.         let directorName = tokens.slice(directedByIndex + 1).join(' ');
  17.         film.director = directorName;
  18.       }
  19.      
  20.     }else if(tokens.includes('onDate')){
  21.       let onDateIndex = tokens.indexOf('onDate');
  22.       let filmName = tokens.slice(0,onDateIndex).join(' ');
  23.       let film = movies.find((o) => o.name === filmName);
  24.  
  25.       if(film){
  26.         film.date = tokens.slice(onDateIndex + 1).join(' ');
  27.       }
  28.     }
  29.   }
  30. }
  31. movie([
  32.   'addMovie Fast and Furious',
  33.   'addMovie Godfather',
  34.   'Inception directedBy Christopher Nolan',
  35.   'Godfather directedBy Francis Ford Coppola',
  36.   'Godfather onDate 29.07.2018',
  37.   'Fast and Furious onDate 30.07.2018',
  38.   'Batman onDate 01.08.2018',
  39.   'Fast and Furious directedBy Rob Cohen'
  40. ])
Add Comment
Please, Sign In to add comment