Start learning free

LESSON 88 OF 410 · CONDITIONAL STATEMENTS - CHALLENGES

Absolute Difference in JavaScript Without Math.abs()

Practice Read the full explanation

The absolute difference is a measure of the difference between two numbers but expressed as a positive number.

In other words, it’s taking the difference between two values (x - y) and then calculating the absolute value of the result.

Create a function (called as absDiff in the examples below; use the name from the starter code) that takes in two numbers a and b and returns their absolute difference.

Examples:

absDiff(3, 0) ➞ 3

absDiff(0, 3) ➞ 3

absDiff(10, -1) ➞ 11

absDiff(-5, -7) ➞ 2

absDiff(6, -6) ➞ 12

absDiff(7, 7) ➞ 0

Note:

Do not use the absolute value function provided by JavaScript (it will defeat the purpose of the challenge)


Hints:


If a is less than b, the absolute difference equals b - a. What if a is greater than or equal to b?

If a < b, we should return b - a. Otherwise, we should return a - b. We can use an if statement to put this in code.
STEP 1 OF 3
    Ctrl + Enter to check

    JAVASCRIPT · CONDITIONAL STATEMENTS - CHALLENGES

    Recreating Absolute Difference in JavaScript, explained

    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 a is less than b, then b - a is positive, so that is the answer.
    • Otherwise a is greater than or equal to b, and a - b is 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 is 3 - 0 = 3.
    • absDiff(0, 3): 0 is less than 3, so the result is 3 - 0 = 3.
    • absDiff(10, -1): the result is 10 - (-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

    1. Write an if statement with its closing brace that tests a < b.
    2. Return b - a inside it. When a is the smaller number, subtracting it from b gives a result that is not negative.
    3. Return a - b below the if. No else is needed. When a < b is true, the function has already returned. This line runs only when a is greater than or equal to b, so a - b is 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 return ends the function. That is why the second return doesn't need an else around it.
    • Decimals are not exact. JavaScript numbers are binary floating-point values, so absDiff(0.3, 0.1) returns 0.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 ===.
    • NaN in, NaN out. If an argument is not a number, both comparisons are false and the subtraction gives NaN.

    Common mistakes

    • Returning a - b always. It passes absDiff(3, 0) and fails absDiff(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 if and forgetting the final return. For a >= b the function then ends without a value and returns undefined.

    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.
    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free