首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在code.org上,我如何过滤我的列表以显示每个索引?

在code.org上,我如何过滤我的列表以显示每个索引?
EN

Stack Overflow用户
提问于 2021-02-01 07:38:33
回答 1查看 330关注 0票数 0

在code.org上(使用Javascript),我正在创建一个应用程序,它可以(通过RollingStone杂志)过滤所有时间内排名前500的专辑。如何按每十年过滤并在屏幕上显示它?我确信我必须使用各种if/else语句,但我不确定要使用哪种语句。

代码语言:javascript
复制
//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.
EN

回答 1

Stack Overflow用户

发布于 2021-02-02 23:00:36

使用遍历,例如检查列表中每一项的" for“循环。你可以把它们堆叠起来,比如if album# >1900,THen,if album#>1900 listb,等等,因为它会慢慢地过滤掉它们,或者1900< x< 1950

代码语言:javascript
复制
    // 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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65985398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档