Soy nuevo en javascript y me han pedido que haga esto; Tarea: cree una matriz de al menos cinco objetos según el título de la película y la fecha de lanzamiento. Título Fecha de lanzamiento Jaws 1975 ET 1982 Psycho 1960 IT 1990 Vertigo 1958
Luego, cree un ciclo que recorrerá la matriz y mostrará en un cuadro de alerta las películas que se estrenaron después de un año en el que el usuario ingresa. Por ejemplo, si el usuario escribe '1978', los cuadros de alerta mostrarán títulos de películas solo para ET e IT.
Esta es mi forma de hacerlo, pero estoy seguro de que hay mejores formas, ¡así que pido un poco de ayuda! Gracias :)
movies = ["Vertigo", " Psycho", " Jaws", " ET", " 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; } } }Intenta usar el objeto de las películas:
movies = [ { title:"Vertigo", year:1958 }, { title:"Psycho", year:1960 }, { title:"Jaws", year:1975 }, { title:"ET", 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."); }Ejemplo de CodePen: https://codepen.io/marcinbzone/pen/popYBaJ
Básicamente, hizo un código duro, envolvió todos estos años y nombres de películas en obj y repitió cada clave y valor.
movies = { 1958 : "Vertigo", 1960 : " Psycho", 1975 : " Jaws", 1982 : " ET", 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));Sin modificar su estructura de datos, esta es una buena manera:
Primero filtre las películas según la entrada del usuario:
const filteredYears = year.filter(item => Number(item) >= Number(userInput);Luego obtenga la película para cada año filtrado.
// 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]); } });