New answers tagged

6

Welcome to Code Review. The CSS looks quite good. I haven’t seen too many style sheets with color lightsalmon 😃. Below are some suggestions. Cache DOM references ”...DOM access is actually pretty costly - I think of it like if I have a bridge - like two pieces of land with a toll bridge, and the JavaScript engine is on one side, and the DOM is on the ...


6

Variables Always use a variable declaration when first declaring your variable. If you don't your variable will be put into the global scope, which is almost never what you want. It also makes it more clear that you don't want to re-assign to an already existing global variable. So for starting_day, for example, put a var, let, or const in front of it. HTML ...


4

Other answers have covered the algorithm so this answer will cover other aspects of review. Good things Strict equality comparisons are used indentation is consistent variables have limited scope Bug with object length Let’s look at that first function: function countA(str) { return (str.match(/a/g) || {}).length; } String.prototype.match() returns “An ...


12

The most important insight that can be made here is that when there are as to split by, it's only the span of text between the end of the first match and the start of the last match that matter, and the rest is a simple counting problem based on the substring lengths. Look at a few examples. Input: babaa ba // This is segment between the end of the first ...


6

Your algorithm is cubic against the length of the string, as it uses nested for loops that each run proportionally to the length of the string with a linear time countA function inside them. But it is possible to achieve a linear time algorithm. Note first that you will always be putting the same letter a characters in each split regardless of the split ...


0

Looks good! I would say maybe the following styles would be just a bit more readable, easier to follow. We can also alter l1 and l2 with more descriptive variable names. Line Counting Fallacy: Sometimes, Line/character countings are helpful for command line languages/scripts (awk, grep, sed, regex, etc.) or maybe Code Golfing, is not a JavaScript ...


3

Edge cases In the real world, input isn't always well-formed. What happens if there is a typo in the data? For example: const data = [ "(2) 2020-09-15", "(3] 2020-09-16" ]; This will lead to undefined appearing in the output for the dates. In other cases/frameworks/languages an exception might be thrown that could crash your script/...


1

I would use something like this: const data = [ "(2) 2020-09-15", "(3) 2020-09-16" ]; let pairs = []; const regex = /^\((\d+)\)\s+(\d{4}-\d{2}-\d{2})$/; for (let datum of data) { // Idea from the previous answer: let pair ...


4

Rather than declaring the variables with let (you should always prefer const) and concatenating and reassigning, consider creating arrays of numbers and dates instead, eg: [2, 3] and ["2020-09-15", "2020-09-16"] Then after the loop is done, join all elements by newlines. For the regular expression, rather than split, I think match would ...


1

Question Responses Using and tracking this is confusing at times. Have I been over-the-top in my use of this? I don't feel it is "over-the-top" though storing a reference to this in another variable is a sign that context isn't bound properly. Bear in mind that an arrow function "Does not have its own bindings to this or super, and should ...


1

Overall Remarks I must admit that this code makes use of more bitwise operators than I normally see in JavaScript. Nonetheless it looks to be sophisticated. There is a lot of code so the remarks here may not be comprehensive but I’ll cover what I can. Question Responses Is Object.freeze() the normal way to implement enum logic in JavaScript? That seems to ...


2

This solution may not be appropriate for your project depending on how else commands are used in your codebase, or if the commands are provided by an external library, among other things. But based on the snippet you give here, it might be a more suitable way to structure your code. Let's start by reviewing two key elements of your code here: commands and ...


7

My answers will comport several steps. Some steps won't be present anymore in the final answer, but I mention them because you might find interesting to follow that in other contexts. Multiple usage of the same reference to command.parameters[0], command.parameters[1], ... You are using command.parameters[0] literally everywhere. If what you're writing look ...


8

Spread Similar to Mohammed's answer, but without mutation and with spread syntax, which I find easier to read than .apply, you can do something like: const executeModeration = async (command, channel) => { await moderation[command.command](...command.parameters); } Default parameters You use a number of conditional operators when calling functions, ...


5

Like Mohammed suggested, Function.apply() could be used to replace the switch statement. Variable naming If the first argument ‘command‘ is an object that contains a string at key ‘command‘ then a better name would be in line for readability- e.g. ‘options‘. Argument use Because Ecmascript-2015 (A.K.A. ES-6) features like arrow functions are used (as well as ...


7

I'm not a JavaScipt programmer, so my suggestions are limited Is there any obvious beginner flaws? Using a large case statement like this is a beginners flaw, because it is difficult to maintain (expansion and contraction are difficult) and performance could be improved. In C++ I would use a map of keywords to functions and I would do something similar in ...


7

You can use Function.prototype.apply(), your code can be like below:- const executeModeration = async (command, channel) => { command.parameters.unshift(channel); await moderation[command.command].apply(null, command.parameters); } you can handle ternary operators logic inside corresponding method.


3

Main question Is there a better, more processor and memory efficient way to do this instead of needlessly throwing the oldest entry at the garbage collector? It seems that calling shift() is the fastest way to remove the first element1 While it is likely not going to make any noticeable difference when iterating over an array with five elements, the order ...


3

Edit 2 Specific to your code/side note of main question: You can create multiple canvas layers. See "Use multiple layered canvases for complex scenes" in Optimizing canvas - Web APIs | MDN End Edit 2 Edit 1 Alternatively: particles.length==5?[, ...particles]=[...particles, particle]:particles.push(particle); var particles=[]; for(var i=0; i&...


4

I think the current code is just fine from an efficiency standpoint. The overhead of a single small object is essentially nothing. Even if 1000 such objects were created and then got GC'd every time the button was clicked, on any remotely modern device, the performance impact would almost certainly be imperceptible. If you really wanted to be more efficient, ...


2

Determining the key pressed The code contains this line: switch ( event.keyCode ) { Looking at the MDN documentation for keyCode we see: Deprecated This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept ...


Top 50 recent answers are included