Hi everyone,
As you may know, there is no way in JavaScript to determine whether or not a number is a multiple of another number, so I wrote this function.
function isMultiple(big, small){
return (big/small + "").indexOf('.') < 0;
}
Now you can use this in your code as well. You're welcome. Got any other tips for me? I am looking for functions, that can ease my programming life. Any programming language is appreciated.
EDIT: Some quick edit, because there seems to be confusion. This introductory text is supposed to be sarcastic. I know there is a %
operator. I know this function does not make sense and that it is super inefficient. The challenge is, that you should provide pieces of code that are (on purpose or not) weired or way too complicated or inefficient or useless.
Top comments (20)
Can I suggest an improvement to your program? It turns out that there is an operator for doing exactly that in JavaScript.
The modulo operator retrieves the remainder of division, which is always 0 for whole multiples.
Hi, thank you for your response. Of course, I know about the modulo operator. This challenge was supposed to be about your worst programs. And as an example I posted this function that divides two numbers, casts the result to a string, searches the whole string for a comma and returns whether a comma was found or not instead of just using
%
. This is pretty bad in my view. I thought you (the community) might have some other terrible examples like this.Ohhhhh. I definitely missed that nuance in your original question. My mistake.
This is not the worst program I've ever written, but it's pretty bad.
I recently participated in the ICPC (International Collegiate Programming Contest) at my school. To get everyone used to the judging system and the idea of a programming contest, it starts with a 45 minute long practice contest with one problem, which is "input a number and output the sum of the numbers from 1 to that number."
Of course, I could have written a program to do that in 5 minutes. But a guy on the team that eventually got first place said, "Theodore, I expect you to do this in the most insane and inefficient way possible." So I wrote this (as best as I can remember):
When you compile this, the C compiler will, after about 30 seconds, generate a lookup table with the answer for any input value up to 1,048,575. This isn't the absolute maximum number you could input though, so as a failsafe, it first calculates the answer at runtime, and then checks if the input is in the lookup table. If it is, then it uses the value from the lookup table instead.
When I came back from the practice contest, I told the other team about what I'd done, and they said "whoa that's the perfect way to do it." They'd written a Python program to generate a C program with the lookup table. They also showed me a screenshot from a judge's machine saying:
haha nice!
I don't have any terrible program ideas at this moment, but here's an equally terrifying scenario for anyone who uses Git.
I Javascript snippet I wrote recently - I really couldn't be bother properly implementing the consumer (of this func) to be async - so I made the async func sync.
N.B. this was in no way production code.
This is still async
Appearances can be deceiving. On a technicality yes it's still async. However, from a callers perspective, it operates synchronously. The debate on if this is synchronous or non-synchronous was not the point of me posting this code. If you need a reminder, see the OP title.
For the caller the function returns on the setTimeout call
Once, about a year ago (maybe less), I was writing a GreaseMonkey/TamperMonkey script to replace all occurrences of a given string in a web page, and I was treating the list of as-of-yet unscanned nodes as a queue. This was before I learned that
Array.prototype.shift
is about as ineffecient as you can get for getting elements out of a large array in JS. Suffice to say I learned the hard way just how bad it getsWith the amount of work that things like
replacers.filter(...)
were doing, even after offloading the actual replacing work until after the loop, scanning the page kangax.github.io/compat-table/es6 (which at the time had roughly 194,000 nodes on it [and has only grown since then]) took anywhere between 60 and 90 seconds on page load.After switching from a queue-based array to a stack-based array (by replacing
nodes.shift()
withnodes.pop()
, the same page took about 4 seconds to scan.I actually had an even worse-designed algorithm before this where I attempted to use nothing but
Array.prototype
methods and ended up iterating the result arrays multiple times, so the function to scan the page had something like aO(15n)
runtime complexity. It was a very readable algorithm, though.One thing that comes to my mind is this factorial implementation using only lambda calculus. In JavaScript. I took the idea from this talk and implemented it in JS.
Had a good chuckle reading through the comments. Although, perhaps you should have posted this as a "share the worst single function snippet you have of a working program", now I imagine those comments would be quite something.. Lol.
Haha yeah agreed, that is a more accurate description of what I want :D
"Write your worst program"? Not sure I follow exactly what you're looking for.
Ok, I've updated the description to better reflect what I mean!
Multiplication should be hard
EDIT Improved version