Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function movies(array) {
- let movieArr = [];
- while (array.length>0) {
- let movie = array.shift()
- if (movie.includes('addMovie ')) {
- // let command = movie.shift();
- let movieName = movie.split('addMovie ')[1]//.join(` `)
- // movieObj.name = movie;
- let movieObj = {
- name: movieName,
- //date: undefined,
- // director: undefined,
- };
- movieArr.push(movieObj);
- }else if (movie.includes(' directedBy ')) {
- // let result = movie.split(` directedBy `);
- let [name, directorName] = movie.split(" directedBy ")
- let found = movieArr.find(el => el.name === name)
- if (found){
- found.director = directorName
- }
- // if (movieArr.includes(result[0])) {
- // movieObj.date = result[1];
- // }
- } else if (movie.includes(` onDate `)) {
- //let result = movie.split(` onDate `);
- let [name, date] = movie.split(" onDate ")
- let found = movieArr.find(el => el.name === name)
- if (found) {
- found.date = date ;
- }
- // if (movieArr.includes(result[0])) {
- // movieObj.director = result[1];
- // }
- }
- }
- movieArr.forEach(found=>{
- if(found.name && found.date && found.director){
- console.log(JSON.stringify(found))
- }
- })
- }
- movies([
- '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 directedBy Rob Cohen'
- ]
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement