Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(moviesArrInput) {
- let movies = [];
- const checkIfExist = (command, string) => {
- let index = string.indexOf(command) - 1;
- let movieName = '';
- for(let i = 0; i < index; i++) {
- movieName += string[i];
- }
- let movieExists = false;
- let existingMovieIndex;
- let data = '';
- for(let index in movies) {
- if(movies[index].name.includes(movieName)) {
- movieExists = true;
- existingMovieIndex = index;
- let commandIndex = string.indexOf(command) + command.length + 1;
- let stringArr = string.split('');
- stringArr.splice(0, commandIndex);
- data = stringArr.join('');
- break;
- } else {
- movieExists = false;
- }
- }
- if(movieExists) {
- return [true, existingMovieIndex, data];
- } else {
- return [false, -1];
- }
- };
- for(let command of moviesArrInput) {
- if(command.includes('addMovie')) {
- let commandArr = command.split('');
- commandArr.splice(0, 9);
- let movieName = commandArr.join('');
- let movie = {
- name: movieName
- };
- movies.push(movie);
- } else if(command.includes('directedBy')) {
- [exists, index, data] = checkIfExist('directedBy', command);
- if(exists) {
- movies[index].director = data;
- }
- } else if(command.includes('onDate')) {
- [exists, index, data] = checkIfExist('onDate', command);
- if(exists) {
- movies[index].date = data;
- }
- }
- }
- for(let movie of movies) {
- if(movie.name == undefined || movie.director == undefined || movie.date == undefined) {
- // dont do shit
- } else {
- console.log(JSON.stringify(movie));
- }
- }
- }
Add Comment
Please, Sign In to add comment