How to check the difference between numeric variables in javascript? The question seems to be elementary, but for some reason there are no considerations. I will explain exactly what you need.

If the numbers are 10 and 20 , then the difference is 10 .

If the numbers are -5 and 3 , then the difference is 8 .

If the numbers are -13 and -15 , then the difference is 2 .

Tell me the algorithm of the function, which if I give two numbers, then the difference between them will return to me.

  • one
    Algortm: take the difference between the numbers: a – b and strip the result of the mark. - Sergiks

2 answers 2

Seriously?

 function get(a, b){ return Math.abs(a - b); } console.info(get(10, 20)); // 10 console.info(get(-5, 3)); // 8 console.info(get(-13, -15)); // 2 

  • why did you call the function "get"? - Sergiks
  • one
    @Sergiks, the first thing that came to mind. They asked for an algorithm, not a creative name. - user207618
  • The most difficult thing in programming is naming and cache invalidation. The rest is nonsense. - Denis Matafonov
  • Actually Math.abs(a - b) is a function - what for to wrap it up in something? - Denis Matafonov
  • @DenisMatafonov, a lot of bukaf, and this method, if you find fault to the end :) - user207618
 function range(a, b){ return Math.abs(ab); }