RRusev77

Movies.js

Jun 26th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(moviesArrInput) {
  2.     let movies = [];
  3.  
  4.     const checkIfExist = (command, string) => {
  5.         let index = string.indexOf(command) - 1;
  6.  
  7.         let movieName = '';
  8.         for(let i = 0; i < index; i++) {
  9.             movieName += string[i];
  10.         }
  11.  
  12.         let movieExists = false;
  13.         let existingMovieIndex;
  14.         let data = '';
  15.  
  16.         for(let index in movies) {
  17.             if(movies[index].name.includes(movieName)) {
  18.                 movieExists = true;
  19.                 existingMovieIndex = index;
  20.                 let commandIndex = string.indexOf(command) + command.length + 1;
  21.                 let stringArr = string.split('');
  22.                 stringArr.splice(0, commandIndex);
  23.                 data = stringArr.join('');
  24.                 break;
  25.             } else {
  26.                 movieExists = false;
  27.             }
  28.         }
  29.  
  30.         if(movieExists) {
  31.             return [true, existingMovieIndex, data];
  32.         } else {
  33.             return [false, -1];
  34.         }
  35.     };
  36.  
  37.     for(let command of moviesArrInput) {
  38.         if(command.includes('addMovie')) {
  39.             let commandArr = command.split('');
  40.             commandArr.splice(0, 9);
  41.             let movieName = commandArr.join('');
  42.  
  43.             let movie = {
  44.                 name: movieName
  45.             };
  46.  
  47.             movies.push(movie);
  48.         } else if(command.includes('directedBy')) {
  49.             [exists, index, data] = checkIfExist('directedBy', command);
  50.             if(exists) {
  51.                 movies[index].director = data;
  52.             }
  53.         } else if(command.includes('onDate')) {
  54.             [exists, index, data] = checkIfExist('onDate', command);
  55.             if(exists) {
  56.                 movies[index].date = data;
  57.             }
  58.         }
  59.     }
  60.  
  61.     for(let movie of movies) {
  62.         if(movie.name == undefined || movie.director == undefined || movie.date == undefined) {
  63.             // dont do shit
  64.         } else {
  65.             console.log(JSON.stringify(movie));
  66.         }
  67.     }
  68. }
Add Comment
Please, Sign In to add comment