Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NOT WORKING
- function movie(input) {
- let movieList = [];
- let directorInfo = [];
- let dateInfo = []
- let result = [];
- for (let details of input) {
- if (details.includes('addMovie')) {
- let addName = details.replace('addMovie ', '');
- movieList.push(
- {
- 'name': addName,
- 'date': "",
- 'director': "",
- }
- )
- } else {
- if (details.includes('directedBy')) {
- let splited_details = details.split(' directedBy ')
- let movieName = splited_details[0];
- let movieDirector = splited_details[1];
- directorInfo.push(movieName)
- directorInfo.push(movieDirector)
- }
- else if (details.includes('onDate')) {
- let splited_details = details.split(' onDate ')
- let movieName = splited_details[0];
- let movieDate = splited_details[1];
- dateInfo.push(movieName)
- dateInfo.push(movieDate)
- }
- }
- }
- for (movie of movieList) {
- for (let i = 0; i < dateInfo.length; i += 2) {
- if (dateInfo[i] === movie.name) {
- movie.date = dateInfo[i + 1]
- }
- }
- for (let j = 0; j < directorInfo.length; j += 2) {
- if (directorInfo[j] === movie.name) {
- movie.director = directorInfo[j + 1]
- }
- }
- if (movie.name !== '' && movie.date !== '' && movie.director !== '') {
- result.push(`
- {"name":"${movie.name}","director":"${movie.director}","date":"${movie.date}"}`
- )
- }
- }
- console.log(result.join('\n'))
- }
- // VS WORKING
- function movieDirectors(array) {
- let movies = [];
- class Movie {
- constructor(name, director, date) {
- this.name = name;
- this.director = director;
- this.date = date;
- }
- }
- let contains = function (movieName) {
- let movie = movies.find(m => m.name === movieName);
- return movie;
- }
- for (let i = 0; i < array.length; i++) {
- let command = array[i].split(' ');
- if (command.includes('addMovie')) {
- let name = command.slice(1, command.length).join(' ');
- movies.push(new Movie(name, null, null));
- } else if (command.includes('directedBy')) {
- let name1 = command.slice(0, command.indexOf('directedBy')).join(' ');
- if (contains(name1) !== undefined) {
- let movie = contains(name1);
- movie.director = command.slice(command.indexOf('directedBy') + 1, command.length).join(' ');
- }
- } else if (command.includes('onDate')) {
- let name2 = command.slice(0, command.indexOf('onDate')).join(' ');
- if (contains(name2) !== undefined) {
- let movie = contains(name2);
- movie.date = command.slice(command.indexOf('onDate') + 1, command.length).join(' ');
- }
- }
- }
- movies.forEach(m => m.director != null && m.name != null && m.date != null ? console.log(JSON.stringify(m)) : null);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement