This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.

# AI solution for JavaScript Coding Question on BFE.dev 129. BigDecimal Division

Solution using long division algorithm:

``function divide(dividend, divisor) {  const MAX_PRECISION = 20; // the maximum digits after decimal point  let sign = 1; // the sign of quotient    if(dividend[0] == '-') {    sign = -sign;    dividend = dividend.slice(1);  }  if(divisor[0] == '-') {    sign = -sign;    divisor = divisor.slice(1);  }    // remove leading/trailing zeros  dividend = dividend.replace(/^0+/, '').replace(/\.?0+\$/, '');  divisor = divisor.replace(/^0+/, '').replace(/\.?0+\$/, '');    if(divisor == '0') throw new Error('Divide by zero');  if(dividend == '0') return '0';    let q = '', r = ''; // quotient and remainder    for(let i = 0; i < dividend.length; i++) {    let d = Number(dividend[i]);    let n = r.length == 0 ? d : Number(r + d.toString()); // pick digit from dividend or remainder    let qn = Math.floor(n / Number(divisor));    q += qn.toString();    r = (n - qn*Number(divisor)).toString(); // update remainder    if(q.length == MAX_PRECISION) break; // precision limit reached  }    if(r.length == 0) return (sign == -1 ? '-' : '') + q; // exact division    // long division for non-terminating decimals  if(q.length == 0) q = "0";  q += '.';  while(r.length > 0 && q.length < MAX_PRECISION+2) {    r += '0'; // pad with zeros    let n = Number(r);    let qn = Math.floor(n / Number(divisor)); // quotient of next digit    q += qn.toString();     r = (n - qn*Number(divisor)).toString(); // update remainder  }    // remove trailing zeros  q = q.replace(/\.?0+\$/, '');  if(q.endsWith('.')) q = q.slice(0, -1);    return (sign == -1 ? '-' : '') + q;}``