Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Better way of looping through an array

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;
  }
}
}

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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));
about 4 years ago · Juan Pablo Isaza Report

0

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]);
    } 
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!