What absolute difference means
The difference between two numbers depends on the order: 10 - 3 is 7, but 3 - 10 is -7. The absolute difference is the distance between the two numbers on the number line, which never depends on order and is never negative. It is the difference with its sign removed. Both absDiff(3, 10) and absDiff(10, 3) give 7.
The method absDiff(a, b) takes two numbers and returns that distance. JavaScript has Math.abs() for this (there is no global abs()), and with it the whole function would be return Math.abs(a - b);. The lesson asks you to build it without that function, using a comparison.
How to find the absolute difference of two numbers
Look at which number is bigger:
- If
ais less thanb, thenb - ais positive, so that is the answer. - Otherwise
ais greater than or equal tob, anda - bis not negative, so that is the answer.
In both cases you subtract the smaller number from the larger one. Checks with the lesson's examples:
absDiff(3, 0): 3 is not less than 0, so the result is3 - 0 = 3.absDiff(0, 3): 0 is less than 3, so the result is3 - 0 = 3.absDiff(10, -1): the result is10 - (-1) = 11.absDiff(-5, -7): -5 is not less than -7, so the result is-5 - (-7) = 2.absDiff(1.5, 1.5): neither is smaller, and the result is 0.
Step by step
- Write an
ifstatement with its closing brace that testsa < b. - Return
b - ainside it. Whenais the smaller number, subtracting it frombgives a result that is not negative. - Return
a - bbelow theif. Noelseis needed. Whena < bis true, the function has already returned. This line runs only whenais greater than or equal tob, soa - bis not negative.
The solution in JavaScript
class Solution {
absDiff(a, b) {
if (a < b) {
return b - a;
}
return a - b;
}
}
The function is a handful of operations, so its time and space are both constant, O(1).
A shorter form uses the conditional (ternary) operator: return a < b ? b - a : a - b;. It gives the same results; the version with if is easier to read while you are learning.
JavaScript details
- A
returnends the function. That is why the secondreturndoesn't need anelsearound it. - Decimals are not exact. JavaScript numbers are binary floating-point values, so
absDiff(0.3, 0.1)returns0.19999999999999998, not 0.2. The function is right; the representation of 0.1 and 0.3 isn't exact. When you compare such results, use a small tolerance instead of===. NaNin,NaNout. If an argument is not a number, both comparisons are false and the subtraction givesNaN.
Common mistakes
- Returning
a - balways. It passesabsDiff(3, 0)and failsabsDiff(0, 3), which gives -3. - Using
Math.abs(). It works in a real project, but the lesson's task is to write the logic yourself. - Returning inside the
ifand forgetting the finalreturn. Fora >= bthe function then ends without a value and returnsundefined.
Key takeaways
- The absolute difference is the larger number minus the smaller one.
- One comparison is enough to decide which subtraction to use.
Math.abs(a - b)does the same job in one call.
