0

在 code.org(使用 Javascript)上,我正在创建一个应用程序来过滤有史以来排名前 500 的专辑(通过 RollingStone 杂志)。如何按每十年过滤一次,然后将其显示在屏幕上?我确信我必须使用各种 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.
4

1 回答 1

0

在列表中使用遍历,例如检查每个项目的“for”循环。那么您可以将它们堆叠起来,例如 if album# >1900, list, 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);
于 2021-02-02T15:00:35.770 回答