New answers tagged

0

Simple solve. Create elems with flags. lets create array of elements with default settings. let be true by. default. And create 1 method for change state of elems. ... public elems = { pid: true, doc: true, rn: true } .... toggle(elem, state) { this.submitOpt = state; elem = state; } in template will use *ngIf and click change state of flags elem &...


1

I'm gonna guess that the code is really trying to find the diff between two tree structures. Futher, I'm assuming from the name "typeObjCopy" that the two trees have identical structure, just different labels. I.e. no added categories allowed. Hopefully this will still be helpful for you even if I'm not entirely correct about this. When I saw the &...


0

In theory, you should not mutate the accumulator. Think of it this way: the callback for reduce can be any function. If we take your callback out of context (i.e. not think about it in terms of reduce), it would look like this: const yourFunction = (acc, fund) => { if(someCondition) { acc.valueA += fund.valueA; } else { acc.valueB += fund....


0

So you have a boolean, and from this boolean you go with either themes.dark or themes.default. Easy, extract a method that takes a boolean and properly calls setThemeAttribute. function setThemeBoolean(bool) { setThemeAttribute(bool ? themes.dark : themes.default); } ... if (currentAutomaticValue) { media.addEventListener("change", this....


1

Define the task I am assuming the task is to find first single instance of a character in the string as this is what the code does. "Repeating" could imply sequential instances of an item. When defining coding tasks always try to avoid any possible ambiguity. Code style General points regarding code style. The variable dict and key can be ...


2

There are some readability concerns to me: startTime, endTime, profit are all arrays. The names should be plural to make this obvious. possibleStarts consists of both start and end. It's confusing. You could instead make an array of job objects with properties start, end, profit. Then you wouldn't need the extra profit lookup either. helper is not a good ...


0

that's my post! I'm glad you were inspired by it. As far as logic goes, this is a great and quite commonly agreed upon way to solve the problem in O(n) time, as long as the language you write it in maintains the order of insertion of a dictionary(which JS does). Well done.


1

Good things The code appears to make good use of ES-6 features like template literals, arrow functions, etc., as well as functional techniques like .map(), .filter(), etc. Indentation appears to be consistent. I agree with Magnus - the ES8 features async/await could help simplify the callbacks. There are just a few suggestions I have to add - see the next ...


2

First of all, kudos for making a single simple file! It was relatively straightforward to read as well, though I think mostly because of the well known api and that there isn't much code. Formatting was nice as well. Here's what I think could improve it: Since you're already using const, promise, arrow functions, etc, I think you could benefit from using ...


1

id attributes Even though this is an electron app and not a typical HTML page, there is a fundamental aspect to the id attribute: it "must be unique in the whole document."1. Consider using a class attribute instead of id, or else use a unique identifier - possibly existing property or new property incremented from a given integer. Textarea ...


1

The real world You can test one style against another and learn nothing about which is the more performant. Benchmarking JS code only has meaning when you are testing an actual real world example. Why, because even the smallest seemingly inconsequential change can effect performance. Things like using a const or a literal, a var or a let, a zero 0.0 or a ...


1

The easy stuff: Remove unnecessary comments. loading view teams panel - This comment adds nothing. If anything, it tells me that your code is not organized properly since you have to add these reminders. In the repo you have many more examples (for example // JSON variable). In my opinion you can remove basically all your comments and have it more readable ...


1

Reducing source complexity There is a rule in coding, "If it works leave it alone!" However when developing code you should aim for simplicity in code, that includes keeping code D.R.Y. (Don't Repeat Yourself). Its way to late to write good code if you need to change working code. Drying out your code Suggestions to reduce complexity. Use ?. (...


2

REVIEW As a solution it is simple and performant which is the aim of all good code. However there are many style issues likely due to inexperience Code style review Way too many comments. Many of them stating the obvious, some inaccurate. Comments should only be used when the code can not describe what the code does, and generally when the code is such ...


1

The problem is metaphorically to assign 9 persons to 9 seats (each person their own seat). The seats come in groups of 3. There are 362880 seating arrangements, but only 280 canonical ones because: A) Reseating the persons within one group doesn't essentially change the arrangement. B) Reordering the groups also doesn't essentially change the arrangement. ...


1

Your questions: You could use BaseAnimal, but that would only give you the base properties. There's no way I know of to make this work other than casting, but that would make it unsafe. The discriminated union is quite nice imo, it's very easy to understand. Since typescript is just types, there is no way for it to actually set a value on an object. You'd ...


1

Events or messages? Is this a messaging system or an event system. As I see it, it is a messaging system. Assuming this is for the browser you may prefer the provided events.Event API which will use the event stack. NOTE check comparability before using. Review A general style code review. Code style is subjective and thus these points are just suggestions ...


2

I couldn't find any obvious bugs, so that's good! For improvements, I have a few comments. It's mostly about readability, and of course it's just my subjective experience when I read the code: If handler is not a function, you should throw an error. It's obviously a mistake. When extending base classes you should not rewrite the extended method signatures. ...


1

I think the way you have created the element is correct but it needs to to be added to DOM. Add the script element to the DOM using document.body.appendChild(js): If you want it to be added at top try doing something like this let myScript = document.createElement("script"); myScript.setAttribute("src", "https://www.example.com/foo....


0

Your questions: Yes, but you need to add some useCallbacks. See below AFAIK, react renders instantly when you set state (for now), but you shouldn't rely on it. For one thing, multiple setState's will be batched together if in the same click listener (and even more places in react 18). Also, with the upcoming concurrent mode we'll get partial renders which ...


1

As it stands your controller is pretty much pointless since it's the service that decides on the api's output model and status codes. The service is also responsible for the db model transformation, i.e. it converts the db model directly to the api model. However, you have stated that the service is responsible for business logic. It is not setup to do this ...


0

What I would suggest is to wrap createStackNavigator in another function where you mimic the api but with different defaults.


2

Recursion There are many reasons to avoid recursion in JavaScript. Recursion is an inefficient way to implement simple loops. Generally you use recursion to follow tree like paths where the path is dynamic or unknown, in this case the path is pre-determined and there is no need to stack the previous loop state as you iterate. A strong argument to NEVER use ...


2

Not really shorter, but something can be done to make it more readable. Do some cleanup Is console.log(response) really needed after the debugging is done? Validate data before processing You're checking for response not being falsy on every loop. Probably you should check it first and only then proceed to all other actions. This if(!response.data) ...


0

Sort! Never use a sort to get a max or min. Sort is only used to order all items in the array. If you want a max in an array you can use Math.max(...[1,2,3,4,5,6,5,4,3,2]) expresses 6. If you need an associated value linked to a max or min you will need to iterate manually storing the max and associated data as and when you find new values. Rewrite The ...


0

I have modified the answer of the HandsomeCoder. This is actually O(n) because object access is O(1) const NUMBERS = [2, 2, 2, 2, 2, 4, 5, 5, 5, 5, 5, 5, 5, 5, 9]; function getMode(array) { function buildFrequencyMap(array) { return array.reduce((acu, number) => { acu[number] = acu[number] !== undefined ? acu[number] + 1 : 0 ...


1

In order to improve the performance, you can reduce the complexity of the code. Currently, the complexity of your approaches is O(n² log(n)) as the sort function is O(n log(n)) and in each iteration, you are using filter or reduce which has a complexity of O(n). I can suggest one approach where you don't need to sort the elements and so the complexity of the ...


3

What you are doing is a breadth-first traversal of a tree. Asserting the prior that there are no cycles or merging branches (nodes with multiple parents), it can be done like this by repeatedly making a list of the next values to be visited in the traversal. function breadthFirstTraversal(pairs) { const rhs = new Set(pairs.map(p => p[1])); const ...


3

Defaults & Ternaries There is nothing wrong with statements. However Javascript and most C like languages allow a wide variety of branching styles. For example you can default the navigation, and use a ternary ? (Conditional operator) to select the redirect option. This has only 1 statement if to allow the local storage key to be set. The default ...


0

You are iterating over the files (or same length) array at least three times, when one time would suffice. You can avoid a lot of clutter with destructuring assignments. The JSON.stringify’s are superfluous and expensive (as already mentioned). A RegExp could fit better for splitting. Below is some code illustrating the above points: const separator = /[...


1

I would suggest breaking out decode into dedicated functions. This way, the code is much more readable, and you can easily find which pieces need to be optimized. For instance, in the following snippet, I split out decoding into types. You can clearly see that decodeObject may not be the most optimized, as its reduce operation does some copying, which can be ...


1

Review This review only looks at the JavaScript. Style Opening { on the same line. else on the same line as closing } Declare constants with const JavaScript naming convention is camelCase. Avoid using snake_case Be consistent with semicolons. Avoid using continue as it breaks the flow. Avoid using break when not needed. Space before the ( for tokens....


0

Refactoring I have refactored your code to be more understandable. I have mainly extracted what you commented into functions and changed what you had in tuples into objects. Here is the result: function generatePossibleFlightsFromCities(flights) { const map = {} for (let i = 0; i < flights.length; i++) { const [origin, destination, cost] = ...


2

I've done a lot of sql + stored-proc over the years. Some things to watch out for: data formats : Dates / Times (Timezone?), integers (7,123.01 v. 6.321,7 v. 5.600e-3 etc), sizes of numbers (and length of number-strings), etc. text encoding : Ascii (José ??), UTF-8, "windows quotes", Win1251, KOI8, GB18030, EBCDIC, etc etc security : your DB ...


0

I think your middleware is fine. The problem is that you do everything in the controller. The controller should just call the login function from your business layer. You had to comment the code to explain it. Treat this as a sign that the function is doing too much. You will see, that I have basically just split the code into the functions based on your ...


0

Well in your specific case, you are implementing the functionality of a calculator. The calculator has a public interface of what it can do. I do not think it makes sense to hide difference just because it is not used anywhere. You can still export it regardless of it being used or not. Also, you seem to be testing functions (implementation) instead of ...


5

One way to simplify As far as I can tell, each of your RegExps just looks at the start of your string for certain exact content - at that point you probably don't want match but startsWith, and you probably don't want to compile RegExps when you can just use the strings you're looking for directly It seems to me like you can get rid of look entirely and ...


Top 50 recent answers are included