Questions tagged [javascript]
JavaScript is primarily a language focused on web development. Being a monopoly on the front end and having Node on the back end. Use this tag for questions regarding vanilla JavaScript; optionally tagged with an ECMAScript version. If you are using a preprocessor such as TypeScript please tag with that too.
9,486
questions
126
votes
4
answers
24k
views
Celebratory fireworks animation
To celebrate an important event, I hastily cobbled together an HTML canvas-based animation.
A few concerns I have include:
Performance: Does it run reasonably smoothly on most modern machines? How ...
98
votes
9
answers
182k
views
Generating all combinations of an array
I'm generating all combinations of an array, so for instance, ["a", "b", "c", "d"] will generate:
...
93
votes
7
answers
186k
views
Convert object array to hash map using lodash
The use case is to convert an array of objects into a hash map where one property is the key and the other property is the value. Common case of using this is converting a "link" object in a ...
89
votes
6
answers
7k
views
You hate h̶a̶t̶s̶ reputation? Click here to get rid of it
Soo... I recently read a meta-post about the inherent problems of reputation. It listed some things like:
Confirmation bias.
Demotivating for low-rep users.
Hubris and elitism of high-rep users over ...
66
votes
4
answers
227k
views
Grouping elements in array by multiple properties
During work, I was given this task: to group elements with similar properties in the array.
In general, the problem is as follows:
...
62
votes
10
answers
178k
views
Given an array of integers, return the smallest positive integer not in it
Write a function:
function solution(A);
that, given an array A of N integers, returns ...
61
votes
5
answers
259k
views
String.IsNullOrEmpty in JavaScript
I am aware of it being frowned upon to do something like write C# in JavaScript. (see this if you don't know what I'm talking about)
But as a judgement call, I think we could stand to have a ...
57
votes
5
answers
37k
views
Verifying password strength using JavaScript
I have a function which verifies the "Password" field and suggests the user to enter a strong password. I also have a label named "Password Strength", referring to the strength of a password (very ...
54
votes
6
answers
48k
views
Using var self = this or .bind(this)? [closed]
I was curious which way is generally preferred, or if there is even a preference, given these two options:
...
52
votes
7
answers
3k
views
Who's in the fellowship? When are Frodo, and the rest of the gang, together
As a variant on the fizzbuzz concept, and as an exercise for learning JavaScript, HTML5, and CSS (I know none of them very well).
The regular fizzbuzz is somewhat tired, but having a web-based, ...
47
votes
4
answers
9k
views
Conway's Game of Life in JavaScript
I made Conway's Game of Life in JavaScript and was hoping someone could give me some pointers regarding my logic of checking adjacent cells. I know there must be a better way, but at the same time, it ...
44
votes
2
answers
40k
views
Balanced parentheses
Given an expression string exp, write a program to examine whether the
pairs and the orders of
"{","}","(",")","[","]"
are correct in exp.
For example,...
43
votes
3
answers
44k
views
Partitioning an array based on a condition in Javascript
Using the array.filter function, I can efficiently pull out all elements that do or do not meet a condition:
...
43
votes
5
answers
31k
views
Determining if two words are anagrams
I was given a this problem at a technical interview and failed miserably in the time given. Afterwards, I sat down and worked out what I think is a good solution. The task was actually to build an ...
41
votes
8
answers
17k
views
Rendering a person's time clock record in HTML, with many color-coded conditions
I have coded this mess of JavaScript and a bit of jQuery. Everything here works perfect as intended, however it is a real mess. What would be the best way to clean this up with best practices and ...
40
votes
1
answer
490
views
Beta-Blue B. Gone
I've written a Code Review design using Userscripts and CSS.
It works by fetching the CSS from my GitHub repo, and using the built in gm_addStyle function to add ...
39
votes
5
answers
7k
views
JavaScript binary search
I wrote an implementation of binary search in JavaScript earlier for kicks, but I noticed my version was significantly different than those found on Google. Here is an example of binary search I found ...
37
votes
5
answers
3k
views
When "How To Ask" is too subtle
This is the first JavaScript code I've ever written, alert("hello, world!"); aside.
This site is closing quite a lot of questions because people won't read the ...
37
votes
2
answers
3k
views
Random clothes generator
For the past four years I've attended a private school which requires its students to wear ties. I've found that it's far easier to figure out what combination of shirt, pants, and tie that go ...
36
votes
4
answers
3k
views
Regex to parse horizontal rules in Markdown
I'm the current maintainer of Showdownjs, a markdown parser off which PageDown (stackexchange's markdown parser) is based on.
Showdown uses the following regex to parse horizontal rules:
...
36
votes
4
answers
1k
views
Memory with a twist
Description
This is the good old game Memory with a twist: Every time you pick a wrong pair, the two tiles you chose will switch their location. So sometimes you might think that a tile is at one ...
33
votes
6
answers
3k
views
To 'this' or not to 'this'?
I was given a homework and I have 2 solutions: one that uses this and other one that doesn't.
I tested both solutions on jsPerf but sometimes it says the version ...
33
votes
4
answers
3k
views
Snowfall in HTML
I am a beginner and I have made Snowfall in HTML for my mom. I'm pretty sure it will not look that awesome to any developer out there, but hey, that's why I've posted it.
I'd like a general review of ...
33
votes
1
answer
73k
views
Pack and unpack bytes to strings
I need to write a function that "packs" an array of bytes (integers between 0 and 255) into a string. I also need to be able to perform the reverse operation, to get my byte array from the string that ...
31
votes
1
answer
457
views
Managing network address information
I'm a developer at a networking company that really has no peers that work above me that I can use for any sort of sounding board for my code, so it's just me. I was wondering if anyone would be ...
30
votes
3
answers
1k
views
Code indentation for declaring inputs/outputs in an array
I'm writing a list of inputs and outputs for to be compared in unit tests.
...
30
votes
2
answers
812
views
Desktop Notifications for flags and queue items
After our recent election, I got elected as a moderator here on Code Review (Thank you, community members!). One of the first things I missed among the moderator tools was desktop notifications for ...
30
votes
1
answer
1k
views
Javascript Minesweeper (with Stack Snippets!)
As Stack Snippets are live on Code Review now, I wanted to do something quick in it as part of rolfl's challenge on meta. And when you make things quick, they probably get dirty. And when things get ...
30
votes
3
answers
17k
views
Sandbox or safely execute eval
In a large project of mine, I've run into a situation where a client might need to run evaluated JavaScript code. I know, it makes me cringe too. One option is to manually parse it, but for future ...
29
votes
2
answers
63k
views
Recursion vs iteration of tree structure
Some recursive code is part of a particularly slow path of a project. Out of curiosity I was playing around with reimplementing the code using stack context iteration instead of recursion. Below are ...
28
votes
5
answers
66k
views
Drawing a grid on canvas
I have created canvas (800x400) - and filled it with a grid. It works but the rendering of the grid (lines) takes around 3 seconds, which seems excessively long. Am I doing something wrong?
...
28
votes
4
answers
4k
views
Self-playing Baseball game
The Demo for what I have made is located here
Basically I am trying to make a self playing JavaScript baseball game with the use of no libraries.
Is there anything I could improve on in the code?
<...
27
votes
7
answers
8k
views
Trapping Rain Water
This task is taken from Leetcode:
Given n non-negative integers representing an elevation map where the
width of each bar is 1, ...
27
votes
2
answers
32k
views
Capitalize the first character of all words (even when following a '-')
I got it working, all variations are displayed Sint-Anna as should be, but I wonder, is there a simpler way to this, since it looks very cluttered?
...
26
votes
2
answers
133k
views
Extract numbers from a string-Javascript
Consider the following string in javascript:
var string="border-radius:90px 20px 30px 40px";
I want to extract the 4 numbers from that string and store them in ...
26
votes
3
answers
2k
views
WebTorrent player
I have been working with HTML5 and CSS3 for a long time now, but have avoided JavaScript because of my belief that it's most frequently used unnecessarily while having a tendency to be poorly written. ...
25
votes
9
answers
45k
views
Set of jQuery .onclick functions
I am hiding and fading in different content on the same page using jQuery to hide() and fadeIn() the content depending on which ...
25
votes
5
answers
842
views
Templating libraries' intelligibility and their performance (compared to mine)
I've just started writing my own little templating library in JavaScript, because as I went through others, there is always this voice in head, which says: "Oh, this is a lot of code and functionality....
25
votes
3
answers
3k
views
Simple object-oriented calculator
After studying several ways of doing OOP in JavaScript I think I finally came up with one that seems OK to me. Is it okay? Do you see some problems I can face by using OOP in JavaScript like this?
...
25
votes
3
answers
718
views
Code-Q2A - copy code blocks from questions to answers
I came up with an idea for a userscript to simplify writing answers on Stack Exchange while writing a previous answer.
This userscript adds "Review"-links on the top of each code segment. When ...
24
votes
7
answers
10k
views
FizzBuzz in Javascript
I'm sure everyone here knows what FizzBuzz is. I would like constructive criticism for my solution.
I'm a beginner to programming as a whole and this isn't my first solution, but it's what I think is ...
24
votes
3
answers
8k
views
Converting an IP
I've written a DNS tool in Node and would like to know if there's a better and more efficient way of handling the conversion from an IP to a long. I'm not too good at bitwise just yet and would like ...
24
votes
6
answers
5k
views
Rock, Paper, Scissors, Lizard and Spock
I am currently done with this little Rock, Paper, Scissors, Lizard, Spock game in JavaScript. The game is working fine, but I am not pleased with the code. Is there a way to refactor these ...
24
votes
3
answers
13k
views
Usage of the ternary "?:" operator with functions listening to click events
I've recently been doing some mods to some old code I've been maintaining for a couple of years now.
As part of a wider set of scripts using YAHOO YUI 2.2 (yes, that old) for dialog-style panels, I ...
24
votes
4
answers
53k
views
Find next occurring Friday (or any dayOfWeek)
Given a particular Date, want to find the next occurring Friday. I solved it using the below code. Various tested scenarios work good for me.
Are there any ...
24
votes
1
answer
40k
views
Handling Hover Events on a Touch Screen
A website I have designed uses a nav menu that shows submenus on :hover. The initial site did not use any responsive design: it targeted only the desktop ...
23
votes
3
answers
141k
views
23
votes
1
answer
2k
views
Brainfuck interpreter in JavaScript
Just what it says on the tin: a brainfuck interpreter in JavaScript.
...
23
votes
1
answer
6k
views
Writing a better alternative to jQuery Autocomplete
Over the last months I have been writing a jQuery plugin called Better Autocomplete (Code on Github). It originated from another project, a Drupal module called Linkit, but I decided it should be a ...
22
votes
6
answers
7k
views
To-do list in jQuery
I'm making a simple to-do list with jQuery. This is my first step in JavaScript and jQuery. How can I improve my code? User enter tasks in input field, submit by pressing 'Enter'. On double-click, he ...