This solution is generated by ChatGPT, correctness is not guaranteed.

AI solution for JavaScript Coding Question on BFE.dev
115. implement BigInt division

Solution:

Here's one possible solution to the problem:

function divide(a, b) {
  const sign = Math.sign(a) * Math.sign(b);
  a = Math.abs(a);
  b = Math.abs(b);
  let result = "";
  let part = "";
  
  for (let i = 0; i < a.toString().length; i++) {
    part += a.toString()[i];
    let quotient = Math.floor(parseInt(part) / b);
    
    if (quotient > 0 || result.length > 0) {
      result += quotient.toString();
      part = (parseInt(part) % b).toString();
    }
  }
  
  if (result === "") {
    result = "0";
  }
  
  return sign === -1 ? `-${result}` : result;
}

First, we determine the sign of the result by multiplying the signs of the two inputs. Then, we take the absolute value of each input and proceed with the division algorithm.

We iterate through each digit of the dividend (the first input) and accumulate it in part, which is a string representation of the current partial dividend. We then divide part by the divisor (the second input) and floor the result to get the quotient.

If the quotient is greater than 0 or we've already accumulated some digits in result (meaning we're past any leading zeros), we add the quotient to result and update part to the remainder (also as a string).

Finally, we handle the case where the input is zero by returning "0" as the result.

Note that this implementation uses the built-in Math.sign() and Math.floor() functions, which are not available in all JavaScript environments. However, they are widely supported and can be easily replaced if necessary.