我对javascript很陌生,有人要求我这样做;任务:根据电影标题和发布日期创建至少由五个对象组成的数组。标题发布日期: Jaws 1975 E.T. 1982心灵1960年IT 1990 Vertigo 1958
然后,创建一个循环,循环遍历数组,在警报框中显示用户输入的一年后发布的电影。例如,如果用户键入‘1978’,那么警报框将只显示E.T.和IT的电影标题。
这是我的方法,但我相信有更好的方法,所以我要求一点帮助!谢谢您:)
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;
}
}
}发布于 2022-04-21 20:48:12
基本上,您做了一个硬代码,包装了所有这些年和电影的名称在obj和循环在每个键和值。
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));发布于 2022-04-21 20:52:12
在不修改数据结构的情况下,这是一个好方法:
首先,根据用户的输入过滤电影:
const filteredYears = year.filter(item => Number(item) >= Number(userInput);然后得到每一年过滤的电影。
// 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]);
}
});发布于 2022-04-21 20:57:06
试着使用电影的对象:
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.");
}https://stackoverflow.com/questions/71960513
复制相似问题