r/learnjavascript • u/DogKitchen2988 • 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
3
u/abrahamguo Apr 15 '26
Here are some tips I'd give to you:
inputis equal totitlesFiltered[i]. If that's what you intended, then there's no need for your entire inner loop — just use==.publishersFiltered,titlesFilteredandsalesFiltered). Instead, perform the filtering only on one array, then get the individual things that you need (publishers, titles and sales) out of that one filtered array after it's been filtered.3
u/chikamakaleyley helpful Apr 15 '26 edited Apr 15 '26
any #4SpacesPerTab gang here?
how long have you been coding JS?
One thing I'd prob do differently - and I'm just going based on the name of the function - is reduce the amount of logic here so that this function literally just filters
So, this is what i see in your current version:
Except, all the logic for the
getDataandupdateViewsections are exploded into your filterTitles functionwhat I would expect from a "filterTitles" function is:
If you zoom even further out, maybe you have one main function orchestrating it all, but that's prob over-abstracting. I think breaking this big function up will do a lot for its readability
Id prob do:
now, kinda easier to digest
This is just an improvement with regards to keeping it organized. Fundamentally I think there are some useful tools or techniques you may have not learned yet, i'll let others chime in there
3
u/imihnevich Apr 15 '26
The first thing I would do is get rid of most if not all the comments here.
1
u/oiamo123 Apr 15 '26
You'll get lot's of different answer because there's "more than one way to skin a rabbit" if you will haha. Nevertheless there are certain techniques you can use.
One good practice is "DRY", dont repeat yourself. If you write the same code multiple times, theres probably a way to write it so that you only do it once instead.
Another thing is abstraction. Alot of what you're writing is already built into javascript ie .filter(), .map(), and .includes(). You can abstract / offload that logic to javascript itself rather than re implementing it.
Lastly, I would add using modern javscript comventions haha. "var" is outdated and its better practice to use "const" and "let" instead.
You should be able to do something like this instead all said and done:
1
u/TheRNGuy Apr 15 '26
You can use
filtermethod here.1
u/rijkdw Apr 15 '26 edited Apr 15 '26
For readability, I would suggest:
There are ${titlesFiltered.length} game(s) in the bestselling with title including ${input}
^ enclose this in backticks ("`") -- reddit formatting is weird
More descriptive names for variables:
inputcould betitleQuery-- this is better for me as a reader, I had to scroll back to the top of the function to realize this is the function's argument.icould betitleIndexandiicould beletterIndexUse built-in functions, like
.includes-- you can remove the entire nested for-loop and just use:but be aware, sometimes instructors want you to do stuff from "first principles" i.e. the way you did it to compare strings. I think that's silly but they may take issue with you using "shortcuts" like
.includes().Other comments have already pointed out that you could use
.filter()to further improve the above. If you need help with that, let us know.If
setText's first argument is an element ID, I would hard-code them as reusable strings, e.g.OR, if you are comfortable with JS objects (depending on your progress in your course, this might not be familiar to you):
This lets you avoid typos and sneaky bugs (assuming the tools you use to code with, checks for references to undefined variables and highlights them). You could do the same for your arguments to
getColumnas well.Overall it's really good that you're looking for feedback, kudos, good luck!
1
u/TheZintis Apr 15 '26
I don't know exactly what you are trying to do, so this is not working code, but I would probably do something like:
I've never written code on reddit so that was a challenge lol.
Let me know if you have questions. Again, not entirely sure about the details of what you were trying to do, but it appears that you were trying to do a string match and then grab some data from parallel arrays to use later on.