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, ...


29

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 ...


20

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 // @...


14

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) => { ...


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]]; };


12

One improvement would be factoring out the repeated call to this.search.toLowerCase() and storing the result in a block-scoped constant so it: doesn't need to be calculated on every call to .includes() for each iteration of filter() can decrease the length of each line where that value is used That may be a small optimization but it is in line with the D.R....


11

I don't see much benefit to the enum. The Notification.permission string (or the return value of requestPermission()) is already pretty clear: 'denied', 'granted', or 'default'. The specification requires that it be one of those, if window.Notification exists. Passing around a number instead of a more intuitive string seems a bit odd. In Javascript, at ...


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,...


10

Simpler approach Another option is to construct a Date object (this can be achieved by appending a time at midnight to the date) 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....


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 ...


8

Comments The comments such a This is Mean. are pointless. And the comment // To call/run this function we can type mean(Your array of numbers separated with commas ','); is a bit strange. Are they targeting people who don't know basic JavaScript syntax? Then why mention commas, but not the brackets ([...]) needed for an array. It probably would be a ...


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

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) { const countMappings = arr.reduce(function(carry, ...


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

Computers do numbers! Compared to manipulating numbers, strings are very slow and use a lot of memory relative to what is being done. To test if it is a perfect integer const isPerfectInt = num => { var sum = (num | 0) % 10; while (num > 0) { sum += (num = num / 10 | 0) % 10 } return sum === 10; } Improved cache You are caching results ...


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 ...


7

It is really hard to say, but at least for me, your code is fine. It seems readable and has no issues. Perhaps if you want to change the style and make it more "canonical", you could write instead: computed: { getfilteredData() { return this.blogs.filter(blog => blog.name.toLowerCase().includes(this.search.toLowerCase()) || ...


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