Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let movies = [];
- for(let command of input){
- let tokens = command.split(' ');
- if(tokens.includes('addMovie')){
- let filmName = tokens.slice(1).join(' ');
- movies.push({name: filmName});
- }
- else if(tokens.includes('directedBy')){
- let index = tokens.indexOf('directedBy');
- let filmName = tokens.slice(0,index).join(' ');
- let film = movies.find((o) => o.name === filmName);
- if(film){
- let directorName = tokens.slice(index + 1).join(' ');
- film.director = directorName;
- }
- }
- else if(tokens.includes('onDate')){
- let indexDate = tokens.indexOf('onDate');
- let filmName = tokens.slice(0,indexDate).join(' ');
- let film = movies.find((o) => o.name === filmName);
- if(film){
- let date = tokens.slice(indexDate + 1).join(' ');
- film.date = date;
- }
- }
- }
- let filteredMovie = movies.filter((movie) => Object.keys(movie).length === 3);
- for(let film of filteredMovie){
- console.log(JSON.stringify(film))
- }
- }
- solve(
- [
- 'addMovie Fast and Furious',
- 'addMovie Godfather',
- 'Inception directedBy Christopher Nolan',
- 'Godfather directedBy Francis Ford Coppola',
- 'Godfather onDate 29.07.2018',
- 'Fast and Furious onDate 30.07.2018',
- 'Batman onDate 01.08.2018',
- 'Fast and Furious'
- ]
- )
Add Comment
Please, Sign In to add comment