Javascript function to get the difference between two numbers

JavascriptFunction

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

var difference = function (a, b) { return Math.abs(a - b); }

Solution 2 - Javascript

Here is a simple function

function diff (num1, num2) {
  if (num1 > num2) {
    return num1 - num2
  } else {
    return num2 - num1
  }
}

And as a shorter, one-line, single-argument, ternary-using arrow function

function diff (a, b) => a > b ? a - b : b - a

Solution 3 - Javascript

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:

Math.abs(a - b);

vs

difference(a, b);

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

Solution 4 - Javascript

It means you want to return absolute value.

function foo(num1 , num2) {
   return Math.abs(num1-num2);
} 

Solution 5 - Javascript

function difference(n, m){
    return Math.abs(n - m)
}

Solution 6 - Javascript

In TypeScript, if anyone interested:

public getDiff(value: number, oldValue: number) {
	return value > oldValue ? value - oldValue : oldValue - value;
}

Solution 7 - Javascript

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.

function diff( x, y ) {

	if ( Math.sign( x ) === Math.sign( y ) ) {

		return Math.abs( x - y );

	} else {

		return Math.abs( x ) + Math.abs( y );

	};

};

diff( 2, 2 ) // 0
diff( -2, -2 ) // 0
diff( -2, 2 ) // 4
diff( 2, -2 ) // 4

Solution on math.stackexchange.com : https://math.stackexchange.com/a/1893992

Solution 8 - Javascript

defining four functions I made a little benchmark, surprisingly

fn1 = (a,b)=>(a>b)?a-b:b-a;
fn2 = (a,b)=>{let c= a-b;return c>0?c:-c;}
fn3 = (a,b)=>Math.abs(a-b)
fn4 = (a,b)=>Math.max(a-b, b-a)


for(let fn of [ fn1, fn2, fn3, fn4])
 executionTime(()=>{console.log(fn);for(let i=0;i<1_000_000;i++)fn(500_000,i)})
 

 
 
 
 function executionTime(cb){//calculate performance
	const t0 = performance.now();
	cb();
	const t1 = performance.now();
	console.log(`cb took ${t1 - t0} milliseconds.`);
}

Here are the results for 1million iterations:

  1. cb took 11.129999998956919 milliseconds.
  2. cb took 53.420000011101365 milliseconds.
  3. cb took 47.40000003948808 milliseconds.
  4. cb took 48.20000007748604 milliseconds.

But unfortunalelly those are false results, I mean executing one by one gave on the console resulting:

  1. 6.9ms 11.7 7.0 4.4
  2. 7.5ms 4.4 6.4 4.5
  3. 7.8ms 5.1 6.2 6.8
  4. 3.9ms 6.3 5.2 5.2

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMithun SreedharanView Question on Stackoverflow
Solution 1 - JavascriptmykhalView Answer on Stackoverflow
Solution 2 - JavascriptSalilView Answer on Stackoverflow
Solution 3 - JavascriptcmccullohView Answer on Stackoverflow
Solution 4 - JavascriptonurbaysanView Answer on Stackoverflow
Solution 5 - JavascriptfmarkView Answer on Stackoverflow
Solution 6 - JavascriptSkorunka FrantišekView Answer on Stackoverflow
Solution 7 - JavascriptfelixmariottoView Answer on Stackoverflow
Solution 8 - JavascriptnerknView Answer on Stackoverflow