Advertisement
bebo231312312321

Untitled

Feb 28th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. function movies(array) {
  2. let movieArr = [];
  3. while (array.length>0) {
  4. let movie = array.shift()
  5. if (movie.includes('addMovie ')) {
  6. // let command = movie.shift();
  7. let movieName = movie.split('addMovie ')[1]//.join(` `)
  8. // movieObj.name = movie;
  9. let movieObj = {
  10. name: movieName,
  11. //date: undefined,
  12. // director: undefined,
  13. };
  14. movieArr.push(movieObj);
  15.  
  16. }else if (movie.includes(' directedBy ')) {
  17. // let result = movie.split(` directedBy `);
  18. let [name, directorName] = movie.split(" directedBy ")
  19. let found = movieArr.find(el => el.name === name)
  20. if (found){
  21. found.director = directorName
  22. }
  23.  
  24. // if (movieArr.includes(result[0])) {
  25. // movieObj.date = result[1];
  26. // }
  27. } else if (movie.includes(` onDate `)) {
  28. //let result = movie.split(` onDate `);
  29. let [name, date] = movie.split(" onDate ")
  30. let found = movieArr.find(el => el.name === name)
  31. if (found) {
  32. found.date = date ;
  33. }
  34. // if (movieArr.includes(result[0])) {
  35. // movieObj.director = result[1];
  36.  
  37. // }
  38. }
  39.  
  40. }
  41.  
  42. movieArr.forEach(found=>{
  43. if(found.name && found.date && found.director){
  44. console.log(JSON.stringify(found))
  45. }
  46. })
  47.  
  48. }
  49. movies([
  50. 'addMovie Fast and Furious',
  51. 'addMovie Godfather',
  52. 'Inception directedBy Christopher Nolan',
  53. 'Godfather directedBy Francis Ford Coppola',
  54. 'Godfather onDate 29.07.2018',
  55. 'Fast and Furious onDate 30.07.2018',
  56. 'Batman onDate 01.08.2018',
  57. 'Fast and Furious directedBy Rob Cohen'
  58. ]
  59. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement