Home > Software design >  Better way of looping through an array
Better way of looping through an array

Time:04-22

I'm new to javascript and I have been asked to do this; Task: create an array of at least five objects based on movie title and release date. Title Release Date Jaws 1975 E.T. 1982 Psycho 1960 IT 1990 Vertigo 1958

Then, create a loop that will loop through the array, displaying in an alert box, movies that were released after a year that the user enters. For example, if the user types ‘1978’, then the alert boxes will show movie titles for E.T. and IT only.

This is my way of doing this but I'm sure there are better ways so I'm asking for a little bit of help! Thank you :)


movies = ["Vertigo", " Psycho", " Jaws", " E.T", " IT"];
year = ["1958", "1960", "1975", "1982", "1990"];


userInput = prompt("Enter the year of movies you are interested in viewing\nYear: ");
  alert("The year you have selected is "   userInput);

for (movie in movies) {
  if (userInput == year[0]) {
    alert("Here are a list of movies released after; "   userInput);
    alert(movies); 
    break;
  }
  if (userInput == year[1]) {
    alert("Here are a list of movies released after; "   userInput);
    alert(movies[1]   movies[2]   movies[3]   movies[4]); 
    break;
  }
  if (userInput == year[2]) {
    alert("Here are a list of movies released after; "   userInput);
    alert(movies[2], movies[3], movies[4]); 
    break;
  }
  if (userInput == year[3]) {
    alert("Here are a list of movies released after; "   userInput);
    alert(movies[3], movies[4]); 
    break;
  }
  if (userInput == year[4]) {
    alert("Here are a list of movies released after; "   userInput);
    alert(movies[4]); 
    break;
  }
  else {
    alert("There are no movies. Please input the correct information and try again.");
    break;
  }
}
}

CodePudding user response:

Basically, you made a hard code, wrap all these years and names of movies in obj and loop in each key and value.

movies = {
    1958 : "Vertigo",
    1960 : " Psycho",
    1975 : " Jaws",
    1982 : " E.T",
    1990 : " IT",
}

userInput = prompt("Enter the year of movies you are interested in viewing\nYear: ");
alert("The year you have selected is "   userInput);

Object.entries(movies).forEach(
    ([key, value]) => key < userInput &&  alert("Here are a list of movies released after; "   value));

CodePudding user response:

Try use object of movies:

movies = [
  {
    title:"Vertigo",
    year:1958
  },
   {
    title:"Psycho",
    year:1960
  },
   {
    title:"Jaws",
    year:1975
  }, {
    title:"E.T",
    year:1982
  }, {
    title:"IT",
    year:1990
  }
]

userInput = prompt("Enter the year of movies you are interested in viewing\nYear: ");
alert("The year you have selected is "   userInput);

//filter movies by year from userInput
const selectedMovies=movies.filter((movie)=>movie.year>=parseInt(userInput));

//check if any movie in array
if(selectedMovies.length){
  alert("Here are a list of movies released after: "   userInput);
  //get only movies titles to array
  const moviesTitlesToDisplay=selectedMovies.reduce((list,movie)=> list.concat(movie.title),[]);
  alert(moviesTitlesToDisplay);
} else {
  alert("There are no movies. Please input the correct information and try again.");
}

CodePen example: https://codepen.io/marcinbzone/pen/popYBaJ

CodePudding user response:

Without modifying your data structure, this is a good way:

First filter the movies based on user input:

const filteredYears = year.filter(item => Number(item) >= Number(userInput);

Then get the movie for each filtered year.

// Loop the filtered years
filteredYears.forEach(year => {
    // Get the index of current element
    const index = year.findIndex(y => y === year);

    // Just in case, if year found
    if ( index !== -1 ){
        alert("Here are a list of movies released after; "   userInput);
        // The movie of this year.
        alert(movies[index]);
    } 
});
  • Related