Advertisement
PowerCell46

Movies JS

Feb 27th, 2023
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function movies(array) {
  2.  
  3.     for (let index = 0; index < array.length; index++) {
  4.         let currentInput = (array[index]).split(" ");
  5.  
  6.         if (currentInput[0] === "addMovie") {
  7.             currentInput.shift();
  8.             let movieName = currentInput.join(" ");
  9.             let movieObject = {
  10.                 movieName: movieName,
  11.                 printFuncDateFirst() {
  12.                     return `{"name":"` + this.movieName + `","date":"` + this.onDate + `","director":"` + this.director + `"}`
  13.                 },
  14.                 printFuncDirectorFirst() {
  15.                     return `{"name":"` + this.movieName + `","director":"` + this.director + `","date":"` + this.onDate  + `"}`
  16.                 },
  17.             }
  18.  
  19.             let xedni = index + 1;
  20.             movieName = movieName.split(" ");
  21.             let directorFirst = false;
  22.             let dateFirst = false;
  23.  
  24.             while (xedni < array.length) {
  25.                 let currentInput = (array[xedni]).split(" ");
  26.                 if (currentInput[0] === movieName[0] && currentInput.includes("directedBy")) {
  27.                     let directedByIndex = currentInput.indexOf("directedBy");
  28.                     let numberOfDeletes = currentInput.length - 1;
  29.                     numberOfDeletes = numberOfDeletes - directedByIndex;
  30.                     let movieDirectorName = currentInput.splice((directedByIndex + 1), numberOfDeletes);
  31.                     movieObject.director = movieDirectorName.join(" ");
  32.                     if(dateFirst === false) {
  33.                         directorFirst = true;
  34.                     }
  35.                 } else if (currentInput[0] === movieName[0] && currentInput.includes("onDate")) {
  36.                     let onDateIndex = currentInput.indexOf("onDate");
  37.                     currentInput.splice(onDateIndex, 1);
  38.                     movieObject.onDate = currentInput[currentInput.length - 1];
  39.                     if(directorFirst === false) {
  40.                         dateFirst = true;
  41.                     }
  42.                 }
  43.                 xedni++;
  44.             }
  45.             if (Object.keys(movieObject).length === 5) {
  46.                 if(dateFirst === true) {
  47.                     console.log(movieObject.printFuncDateFirst());
  48.                 } else if (directorFirst === true) {
  49.                     console.log(movieObject.printFuncDirectorFirst());
  50.                 }
  51.             }
  52.         }
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement