I want a Simple Javascript function to get the difference between two numbers in such a way that foo(2, 3)
and foo(3,2
) will return the same difference 1.
I want a Simple Javascript function to get the difference between two numbers in such a way that foo(2, 3)
and foo(3,2
) will return the same difference 1.
Here is a simple function
And as a shorter, one-line, single-argument, ternary-using arrow function
It means you want to return absolute value.
I don’t get how this is possible on such a simingly common question, but all the answers I found here are wrong in certain cases.
If both numbers are on the same side of zero then the accepted answer is right, but if the numbers are not on the same side of zero, then their absolute values must be added, not subtracted.
Solution on math.stackexchange.com : https://math.stackexchange.com/a/1893992
Seems odd to define a whole new function just to not have to put a minus sign instead of a comma when you call it:
vs
(with difference calling another function you defined to call that returns the output of the first code example). I’d just use the built in abs method on the Math object.
In
TypeScript
, if anyone interested:defining four functions I made a little benchmark, surprisingly
Here are the results for 1million iterations:
But unfortunalelly those are false results, I mean executing one by one gave on the console resulting:
I think while looping, after each iteration garbage is collecting while other functions doing their bests. While executing each one by one, fn2 came second, while using extra variable( vm is optimizing with register ? ) .
real casino slots https://onlinecasinosure.com/# – play slots online play online casino slot games
royal river casino vegas world free games online best casino slots online zone online casino slots
medterra cbd http://cbdforsalesh.com/# – cbd oil benefits buy cbd oil buy cbd oil online
dating naked https://tinderdatingww.com/# – dating sites free zoosk online dating site speed dating