DEV Community

Ticha Godwill Nji
Ticha Godwill Nji

Posted on • Updated on

How to divide two numbers in JavaScript with out using the "/" operator?

Here're some tips on how to divide numbers in javascript without using the "/" operator. This is a very efficacy method. Which means that it has just a few steps.

I will use ES6 syntax for writing my code.

const divide = (dividend, divisor) => {
    if (divisor === 0) {
        throw new Error("Division by zero is not allowed.");
    }

    let quotient = 0;
    let isNegative = false;

    if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) {
        isNegative = true;
    }

    dividend = Math.abs(dividend);
    divisor = Math.abs(divisor);

    while (dividend >= divisor) {
        dividend -= divisor;
        quotient++;
    }

    if (isNegative) {
        quotient = -quotient;
    }

    return quotient;
}

// Test the divide function
console.log(divide(10, 1)); // Output should be 5
console.log(divide(7, 3)); // Output should be 2
console.log(divide(10, -2)); // Output should be -5
console.log(divide(-7, 3)); // Output should be -2
console.log(divide(0, 5)); // Output should be 0
console.log(divide(2, 10)); // Output should be 0


Enter fullscreen mode Exit fullscreen mode

Breakdown

First, we declare the function divide and pass the dividend and divisor as parameters.

In the function block, we start by checking if the divisor is equal to zero. If that is true, we throw an error message.

The next step is to declare the quotient and assign it to zero, and isNegative and assign it the value of false.

We then check if any of the numbers is less than zero, which makes isNegative return true.

If isNegative returns true, we take the absolute value of the numbers to convert them to positive numbers by using the JS built-in Math.abs().

We now enter a while loop that keeps subtracting the divisor from the dividend, and the quotient is incremented.

If isNegative is true, the quotient should be multiplied by *-1 * and returned as our result.

Watch the video on Youtube

Top comments (12)

Collapse
 
avikki profile image
AviKKi

Alternatively you can do a binary search for the solution in range 0 to dividend in log(n) time

Collapse
 
ticha profile image
Ticha Godwill Nji

Thank you for your comment! Yes, using binary search for division can indeed provide a more efficient solution, especially for large dividends. It's a great way to optimize the algorithm and reduce computation time. I appreciate you highlighting this approach!

Collapse
 
avikki profile image
AviKKi

Alternatively you can do a binary search for the solution in range 0 to dividend

Collapse
 
ticha profile image
Ticha Godwill Nji

Absolutely, it can be used in the** while loop**.
There are many methods that can be used to in accomplish this same results.

Binary

while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (mid * divisor <= dividend) {
quotient = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}

Collapse
 
citronbrick profile image
CitronBrick • Edited

you can enable syntax highlighting through:

triple backtick javascript
code
triple backtick

Collapse
 
ticha profile image
Ticha Godwill Nji • Edited

Thanks for letting me know Buddy!

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

You could also use logarithms

Collapse
 
ticha profile image
Ticha Godwill Nji • Edited

Thank you for your comment! It's a great way to optimize the algorithm and reduce computation time. I appreciate you highlighting this approach!

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

? I never mentioned binary search

// Calculate a/b without /
Math.exp(Math.log(a) - Math.log(b))
Enter fullscreen mode Exit fullscreen mode

Yes, you'll need some code to handle negative numbers, but this highlights the main approach

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ
let a = 5
a /= 2
Enter fullscreen mode Exit fullscreen mode

I didn't use the / operator :)

Collapse
 
kapenike profile image
Spencer

I like it! I wonder if there is a way to speed this up by wrapping the negative number handling into the math πŸ€”

Collapse
 
ticha profile image
Ticha Godwill Nji

That's a great idea! Handling negative numbers within the mathematical calculations can streamline the code and potentially improve its efficiency. One way to achieve this is by leveraging the absolute values of the dividend and divisor during the binary search process, and then adjusting the sign of the quotient accordingly. This approach avoids the need for separate handling of negative numbers.