Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda