Ivankooo1

Movies - Object and Classes - Exs

Jan 5th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.    let movies = [];
  3.    for(let command of input){
  4.        let tokens = command.split(' ');
  5.  
  6.        if(tokens.includes('addMovie')){
  7.            let filmName = tokens.slice(1).join(' ');
  8.            movies.push({name: filmName});
  9.        
  10.        }
  11.  
  12.        else if(tokens.includes('directedBy')){
  13.            let index = tokens.indexOf('directedBy');
  14.            let filmName = tokens.slice(0,index).join(' ');
  15.            let film = movies.find((o) => o.name === filmName);
  16.  
  17.            if(film){
  18.               let directorName = tokens.slice(index + 1).join(' ');
  19.               film.director = directorName;
  20.            }
  21.        }
  22.  
  23.        else if(tokens.includes('onDate')){
  24.            let indexDate = tokens.indexOf('onDate');
  25.            let filmName = tokens.slice(0,indexDate).join(' ');
  26.            let film = movies.find((o) => o.name === filmName);
  27.  
  28.            if(film){
  29.                let date = tokens.slice(indexDate + 1).join(' ');
  30.                film.date = date;
  31.            }
  32.        }
  33.    }
  34.    let filteredMovie = movies.filter((movie) => Object.keys(movie).length === 3);
  35.    for(let film of filteredMovie){
  36.        console.log(JSON.stringify(film))
  37.    }
  38. }
  39. solve(
  40.     [
  41.         'addMovie Fast and Furious',
  42.         'addMovie Godfather',
  43.         'Inception directedBy Christopher Nolan',
  44.         'Godfather directedBy Francis Ford Coppola',
  45.         'Godfather onDate 29.07.2018',
  46.         'Fast and Furious onDate 30.07.2018',
  47.         'Batman onDate 01.08.2018',
  48.         'Fast and Furious'
  49.     ]    
  50. )
Add Comment
Please, Sign In to add comment