# Tag Info

## Hot answers tagged javascript

3

I've noticed you have a common bug that often occurs in Javascript code called a race condition. If uniqueIdCreatorHandler is called again before a previous call finishes there is a race condition created because state.ids is replaced at the end of the call. If both calls happen at the same time they each generate a unique copy of the original state.ids ...

3

Use variable for element, toggle can take multiple classes, use minimal selector. var voteButton; if(type === 'down'){ voteButton = $('.i-down-' + commentid); voteButton.toggleClass('active-color opacity-initial'); } else { var voteButton =$('.i-up-'+commentid); voteButton.toggleClass('primary-color opacity-initial'); } if(voted === '...

3

I would propose something like so (given that you have assumed no dupes, etc) function migrateValues(obj, srcKey, destKey, value) { return { ...obj, [srcKey]: obj[srcKey].filter(w => w !== value), [destKey]: obj[destKey].concat(value) }; } We spread obj, and then overwrite the srcKey and destKey properties. There are ...

3

There are some issues with your class, but I don't think that they are so specific to implementing a strategy pattern. addToPage() { this.statement = document.createElement("p") this.statement.innerHTML=this.flying.fly() document.body.append(this.statement) } This assumes there is a global document variable, and that statement is part of the ...

2

The code in componentDidUpdate looks a little odd to me. if(this.state.deck.length === 0){ You are checking if there aren't any cards, then drawing some cards. This works because you are initialising the deck within getPlayerCard. What I would do is put the deck initialisation within the startNewGame method instead. getDealerCard and getPlayerCard are ...

2

Strategies It would have been good to summarize the key points and strategies of the different implementations. Mostly for yourself, to clarify your thinking and solidify your understanding. Secondly for reviewers :-) Let me take a jab at that now. Solution 1 while there are lists to merge find the minimum head and merge it update the list with the ...

2

Firstly, before I start, I'd like to mention that you might not need these JS animation functions. A lot of animations can be achieved through CSS transitions and keyframes, using JS to toggle classes where needed. In my opinion, JS should only step in when the animation becomes too intensive for CSS to handle. There appears to be a lot of duplication here. ...

2

Your code is pretty sound. I can't see any improvements that specifically use ES5/ES6 tricks - the spread operator for example will set all of the other keys to undefined instead of leaving them empty, which I think is undesired. Further loops would iterate over those undefined keys as well. That said, here are some improvements: Use reduce to remove the ...

1

I don't know too much about what you're trying to do (but it makes sense and I think I get it). Personally my feeling is that all you are missing is the right helper functions to make your code more transparent. I don't know how efficient this should be and hence if the overhead of function calls matters to you, but I am guessing no as otherwise you would ...

1

Short review; Use a beautifier, the code is not properly indented or spaced out You use var, try to use const and let The code already assigned billAmount / noOfGuests to split, so you can compare rounding and split £ should probably be a single string constant elseIf is a terrible variable name elseIf is already multiplied by noOfGuests, why would you ...

1

Interesting question; You can make this code slightly less efficient (however, the user will never notice), and so we can just attach listeners to everything. const addClickEventListeners = () => { const addListener = (button) => button.addEventListener('click', someFunction, false); document.querySelectorAll('[name="multiple_choice"]').forEach(...

1

Here is ES6 implementation. Use arrow function With reduce and using destructure in input function arguments and have the default value for title. In reduce function use destucture to get the values from split result. const data = [{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]; const indexSort = data => data.reduce((acc, {title = '...

1

I figured out a way thru trial and error. I'll post this here for anyone else who could use it. // dismiss -- just creating less CSS const randomColor = "#" + ((1 << 24) * Math.random() | 0).toString(16); document.querySelectorAll('.label').forEach((x, i) => { document.querySelectorAll('.label')[i].style.background = "#" + ((1 << 24) * ...

1

From the advise of MarkM. Finding the min of absolute values of the array. A bit more readable than his approach (personal opinion). function closestToZero(arr) { if (!arr || arr.length === 0) { return 0; } let closestToZero = arr[0]; arr.forEach(function(number) { if (Math.abs(number) < Math.abs(closestToZero)) { ...

1

You have one bug in your code. The binaryGap function is supposed to be side-effect free. Your implementation isn't since it modifies the global variable i. To fix this, apply the following patch: - for (i=0; i<bin.length; i++){ + for (let i=0; i<bin.length; i++){ There are 2 lines in your code that are redundant: if (gaps.length===1){ return ...

Only top voted, non community-wiki answers of a minimum length are eligible