在code.org上(使用Javascript),我正在创建一个应用程序,它可以(通过RollingStone杂志)过滤所有时间内排名前500的专辑。如何按每十年过滤并在屏幕上显示它?我确信我必须使用各种if/else语句,但我不确定要使用哪种语句。
//Declare the variables.
var fiftiesAlbums;
var sixtiesAlbums;
var seventiesAlbums;
var eightiesAlbums;
var ninetiesAlbums;
var twoThousandsAlbums;
//Create the functions.
onEvent("button1", "click", function( ) {
console.log("1950's button clicked!");
setScreen("screen2");
});
onEvent("button2", "click", function( ) {
console.log("1960's button clicked!");
setScreen("screen3");
});
onEvent("button3", "click", function( ) {
console.log("1970's button clicked!");
setScreen("screen4");
});
onEvent("button4", "click", function( ) {
console.log("1980's button clicked!");
setScreen("screen5");
});
onEvent("button5", "click", function( ) {
console.log("1990's button clicked!");
setScreen("screen6");
});
onEvent("button6", "click", function( ) {
console.log("2000's button clicked!");
setScreen("screen7");
});
onEvent("backButton", "click", function( ) {
console.log("Back button clicked!");
setScreen("screen1");
});
onEvent("backButton2", "click", function( ) {
console.log("Back button clicked!");
setScreen("screen1");
});
onEvent("backButton3", "click", function( ) {
console.log("Back button clicked!");
setScreen("screen1");
});
onEvent("backButton4", "click", function( ) {
console.log("Back button clicked!");
setScreen("screen1");
});
onEvent("backButton5", "click", function( ) {
console.log("Back button clicked!");
setScreen("screen1");
});
onEvent("backButton6", "click", function( ) {
console.log("Back button clicked!");
setScreen("screen1");
});
//Filter by each decade.发布于 2021-02-02 23:00:36
使用遍历,例如检查列表中每一项的" for“循环。你可以把它们堆叠起来,比如if album# >1900,THen,if album#>1900 listb,等等,因为它会慢慢地过滤掉它们,或者1900< x< 1950
// Create and assign lists of states and admission years
var stateList = getColumn("US States","State Name");
var yearList = getColumn("US States","Admission Year");
// List of states with admission year after 1900
var since1900List = [];
// Filtering the table
var state;
var year;
for(var i = 0; i < stateList.length; i++){
state = stateList[i];
year = yearList[i];
if(year > 1900){
appendItem(since1900List, state);
}
}
console.log("States added since 1900:");
console.log(since1900List);
// Create and assign list of populations
var populationList = getColumn("US States","Population");
// List of states with population less than one million
var smallPopulationList = [];
var population;
for(var i = 0; i < stateList.length; i++){
state = stateList[i];
population = populationList[i];
if(population < 1000000){
appendItem(smallPopulationList, state);
}
}
console.log("List of state with fewer than one million people");
console.log(smallPopulationList);https://stackoverflow.com/questions/65985398
复制相似问题