r/learnjavascript • • Apr 15 '26

Trying to get better at readability/writing optimal code, need feedback

//Filter lists based on inputed title
function filterTitles(input) {
  //Initializes lists
  var matchingLetters = 0;
  publishersFiltered = getColumn("Best Selling Video Games", "Publisher");
  titlesFiltered = getColumn("Best Selling Video Games", "Title");
  salesFiltered = getColumn("Best Selling Video Games", "Sales");
  //"i" is current index of "titles" being checked
  for(var i = 0;i < titlesFiltered.length;i++) {
    //Reset matched letters before checking next index
    matchingLetters = 0;
    //"ii" is current letter being checked
    for(var ii = 0;ii < input.length;ii++) {
      //Checks if current letter of input is equal to current letter of index i of publishers
      if(input.substring(ii,ii + 1) == titlesFiltered[i].substring(ii,ii + 1)) {
        matchingLetters++;
      } else {
        ii = input.length;
      }
    }
    //Removes index i of all filtered lists if the input string is not found
    if(matchingLetters != input.length) {
      removeItem(salesFiltered, i);
      removeItem(publishersFiltered, i);
      removeItem(titlesFiltered, i);
      i--;
    }
  }
  //Updates the screen to represent filtered items
  setScreen("titles");
  setText("therearetitles", "There are " + titlesFiltered.length + " game(s) in the bestselling with title including " + input);
  setText("titleslist2", titlesFiltered.join("\n \n"));
  setText("publisherslist2", publishersFiltered.join("\n \n"));
  setText("saleslist2", salesFiltered.join("\n \n"));
} 

Done for a school project, filters a set of lists based on a textbox input stored in the "input" argument. Trying to learn good practice and comment writing. Could i have written this more optimally, like maybe without the nested loops?

2 Upvotes

17 comments sorted by

View all comments

Show parent comment

1

u/DogKitchen2988 Apr 15 '26

The code is pretty rushed so i hadn't thought of splitting into multiple functions lol. Also can you explain the two datasets? I'm a bit new to using arrays and not sure how the myData and filteredData and how the const works

1

u/chikamakaleyley helpful Apr 15 '26 edited Apr 15 '26

oh i explain in my other comment here.

honestly don't worry about const right now if your teacher wants you to use var

but basically the idea is you want to always have the full list available, and when you filter it, you're just really creating a new list (a copy), that has been filtered. any time you need the original list, you just reference the 'full' one

and actually - this is kinda nice - it looks like you might already understand and use this concept:

publishersFiltered = getColumn("Best Selling Video Games", "Publisher");
titlesFiltered = getColumn("Best Selling Video Games", "Title");
salesFiltered = getColumn("Best Selling Video Games", "Sales");

whatever getColumn is doing it's basically taking the same source list "Best Selling Video Games" and creating 3 filtered copies of it.

1

u/chikamakaleyley helpful Apr 15 '26

but again, based on the name of the function, now i'm assuming getColumn is some sort of data fetch where you're requesting from a database

in general, you only want to perform this operation once, to reduce the number of trips back and forth btwn your web page and the database

it's more efficient to just ask for the data once, then make a copy each time you filter it.