48

It would be better to do this with a user style, e.g. using the Stylus extension, like this: .rep, .reputation, .reputation-score { visibility: hidden; } One advantage of user styles over scripts is that they're applied as soon as the page starts to load, so you won't see the rep counts flicker briefly while the page is loading. Also, user styles will ...


37

An alternative route is to use string.replace() and Regular Expressions to strip everything but the vowels from the string. Then count the length of the resulting string. This avoids iteration altogether. const vowelCount = s => s.replace(/[^aeiou]/gi, '').length console.log(vowelCount('The quick brown fox jumps over the lazy dog')) The regex ...


29

You could use querySelectorAll instead of getElementsByClassName. That way, you have a bit more flexibility in targeting elements. It also allows multiple selectors. That way, you don't have to loop twice. Instead of [].slice which uses an unnecessary array as host, try using Array.prototype.slice.call instead. Longer, but without that extra array. Also, ...


27

A few comments: 1) fetch already returns a promise, which means this: new Promise((resolve, reject) => { return fetch(url).then(response => { if (response.ok) { resolve(response) } else { reject(new Error('error')) } }, error => { reject(new Error(error.message)) }) }) Is pretty much the same as: fetch(url).then(...


26

I'm just wondering if there's an easier way to solve this problem without the big blocky code in the if statement. Well, you could put all of those in an array: const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']; And then the if condition can be simplified using Array.prototype.includes(): if( vowels.includes(v)) { The fact that I have ...


19

Well, you normally don't want to use the Promise constructor in your higher level code, you want to Promisify as low as possible. So let's create a function that checks for a single image, and resolves whenever you know the status of that image: // When there's only one statement, you can drop the {} and the return // x => y is equivalent to x => { ...


19

You could take an object and check if the action exists. If so, take the value as key for assignment. const actions = { accept: 'showAcceptButton', reject: 'showRejectButton', transfer: 'showTransferButton' }; for (const element of actionsReferences) { if (element in actions) { this[actions[element]] = true; } }


16

Update url UserScripts have the magical ability to update themselves if you provide a link to a raw version of your script as a UserScript header: Note: You actually have to increase the version, or it won't update. // ==UserScript== // @name Reputation Hider // @namespace http://gihub.com/Vogel612/ReputationHider // @version 0.1 // @...


13

Most interesting, In BaseObject, you should really have a constructor that builds this.listeners, it would make your code much cleaner afterwards I am not sure why your listeners are not private in BaseObject ? I am not sure what is more important to you, learn EC6 or position divs efficiently, if the latter is more important, than this code is massive ...


12

To make @kfx's answer more readable: const getFirstCategory = (bonusPlus, prizeCategories) => { const bonusPlusLevel = bonusPlus ? 2 : 1; if (prizeCategories[0].playLevel === bonusPlusLevel) { return [prizeCategories[0]]; } return [prizeCategories[1]]; };


11

As you expected, there is a much simpler way to do this. Create a map where the keys are the numbers in the array and the value is the number of times each number appears in the array. Filter the input array for any numbers that appear less than X times. function removeMany(numbers, max) { const numberMap = numbers.reduce((map, num) => { ...


10

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array. var obj={a:1,b:[1,...


8

So, going to 'answer' my own question here with a nice thing I discovered since then. Might expand my answer if I discover more stuff. In ECMAscript 6 rather than var that = this; something.on("positionchange",function(){ that.reposition(); }); You can do something.on("positionchange",() => { this.reposition(); }); As the this scope doesn't ...


8

warning: cheesy meme with bad pun below - if you don't like those, then please skip it... Question responses Do I have too many / too few components? I am aiming to make several other games in Vue so I like to make things re-useable. I think the current components are divided well. The existing components make sense. How are my Vue skills? Usage of ...


8

Overall you have a good D3 code here. I'm fairly impressed with the questions I've seen here at C.O. lately, from people claiming "This is the very first time I've used d3 or drawn a SVG". Congrats. However, before sharing my proposed alternative, I'd like to tell you that, unfortunately, you're using the wrong tool for the task! As you can see in my ...


8

whenever you encounter a task where you have to combine or permute things, then backtracking is a possible approach. True. It is not necessarily the best though (in fact it is rarely the best). In this particular case, the problem naturally maps to finding the next combination. In turn, it is no more than an increment of a number in some fancy numbering ...


8

You don't need the special case for k == 0. When k == 0, the result of the other expression will be 1 as well. For this sequence of binomial coefficients you don't need to calculate the full fak expression each time. You can also start with c = a ** i, and then, for each k, multiply by (n - k) / (k + 1) * b / a. That's a bit faster and provides less risk of ...


7

There’s no need for recursion here. You have some base probability map, and you want to rotate it such that the maximum element points in some particular direction. You could implement that fairly simply: var INDICES = [1, 2, 3, 6, 9, 8, 7, 4]; var PROBABILITIES = [80, 60, 30, 20, 10, 20, 30, 60]; function generateProbabilities(lastNumpadDirection) { ...


7

When I ran your script and started digging through the dom, I noticed that Apr 21 12:00 AM was missing -- I wonder why that was. Anyhow, I wanted to encourage you to adjust the timestamp values because they are irregularly formatted. Then I started to play with iterated strtotime() calls to generate consistent Y-m-d stamps, but some of the strings needed ...


7

Simpler approach Another option is to construct a Date object (appending a time at midnight) and pass that to Intl.DateTimeFormat.prototype.format(). Then there is no need to parse it and reconstruct it yourself. function formatCourseDate(date) { const dateObj = new Date(date + 'T00:00:00'); return new Intl.DateTimeFormat('en-US').format(dateObj); ...


7

What if you want to add the functionality for up to 3 players? You'd have to create a 'player3AnimationStage, player3SpriteSheet etc. It's also inside the 'gameState', which arguably makes sense, but still could be separated into it's own class. For example: class Player { constructor(spriteSheet, animationStage) { this.SpriteSheet = ...


7

The question states that the input is a number 32 signed int so checking for undefined or null is a waste of time. The solution is a little long. Some of it due to not being familiar with some old school short cuts. To get the sign of a number use Math.sign -JavaScript numbers are doubles but when you use bitwise operations on them they are converted to ...


7

Sᴀᴍ Onᴇᴌᴀ answer has the right idea for small strings, but can be improved by using a Set to hold the vowels rather than an array. This reduces the overhead of Array.includes which will iterate each character in the vowels array for non matching characters You can create a set as const vowels = new Set([..."AEIOUaeiou"]); To encapsulate the constant vowels ...


7

Due to the lack of real context here (and issues I'll mention later), I can't really speak to the algorithm. I'll just focus on style. First, you need to take far greater care in creating meaningful names. This is incredibly hard to comprehend; and naming is the biggest contributor to the problem. Take a step back, pretend you didn't write this, and look ...


6

Instead of document.getElementsByClassName, you can use document.querySelectorAll(CSS selector). Edit(Reply to @200_success): You can use anyone of those. Just I prefer document.querySelectorAll since I think it is better a little. document.getElementsByClassName has 31 letters, and document.querySelectorAll has 25 letters. document.querySelectorAll uses ...


6

I would go with the second one. Not because eval here is insecure -- you're validating the arguments -- but because it's overkill. eval is designed to parse and run any JavaScript code, so it is slow (compared to normal JavaScript code); engines do a lot of optimisation before running code, but they can't optimise if the code is in a dynamic string which ...


6

There are lots of things you can do: Use a parameterized function to create the XHR promises, since your three functions are identical other than what they're requesting Use Promise.all, since none of the requests relies on information from previous requests and they can run in parallel Move the JSON parsing into the functions retrieving the data, rather ...


6

tl;dr The code can be re-written to first count the number of occurrences of each number, then return an array filtering out any number that occurs more than 3 times. // Remove duplicates that occur 3 or more times in an array // keeping unique values and those with less than 3 function removeMany(arr) { let countMappings = arr.reduce(function(carry, ...


6

Disclaimer: I totally understand that my answer is very subjective, but you may still find it useful. I am myself a big proponent of functional programming, and from this perspective your code looks decent. It uses abstractions efficiently, and is DRY. Functional Programming is Great; Too Many Abstractions Is Questionable However, I'm also a big proponent ...


6

First of all I would define a function that takes two valid HTML color strings and returns a valid HTML color string that represents the average of the two. With this approach you could avoid polluting your "color library code" with calls to host objects, which helps for better code reuse. DOM manipulation and input validation (e.g. by a regex) could then ...


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