我是 javascript 新手,我被要求這樣做;任務:根據電影名稱和發行日期創建一個至少包含五個物件的陣列。標題 發行日期 Jaws 1975 ET 1982 Psycho 1960 IT 1990 Vertigo 1958
然后,創建一個回圈遍歷陣列,在警告框中顯示用戶輸入一年后發布的電影。例如,如果用戶鍵入“1978”,則警告框將僅顯示 ET 和 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;
}
}
}
uj5u.com熱心網友回復:
有一個更好的方法:
首先根據用戶輸入過濾電影:
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]);
}
});
uj5u.com熱心網友回復:
基本上,您撰寫了一個硬代碼,將所有這些年份和電影名稱包裝在 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));
uj5u.com熱心網友回復:
嘗試使用電影物件:
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 示例:https ://codepen.io/marcinbzone/pen/popYBaJ
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/460648.html
標籤:javascript
上一篇:雙因素身份驗證程序應該在哪里?
