How can I perform JavaScript integer division and get the remainder separately?

Nice tips! If you’re into more of a mathematical approach or just like keeping things low-level, you could also manually compute the quotient by repeatedly subtracting b from a. It’s more work than the built-in methods, but it’s a fun way to see how integer division works under the hood.

Here’s how you could do it:

function divide(a, b) {
    let quotient = 0;
    while (a >= b) {
        a -= b;
        quotient++;
    }
    const remainder = a;
    return { quotient, remainder };
}

const result = divide(11, 3);
console.log(result.quotient);  // 3
console.log(result.remainder); // 2

In this version, the function keeps subtracting b from a until a is smaller than b, and it increments the quotient every time. The remainder is whatever’s left in a. It’s not as optimized as using Math.floor() or bitwise tricks, but it gives you a clear understanding of how JavaScript integer division works at its core without relying on built-in functions.