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

AI solution for JavaScript Coding Question on BFE.dev
128. BigDecimal multiplication

Solution using the Grade-school algorithm:

function multiply(a, b) {
  const sign = (a[0] === '-' ? b[0] === '-' ? '' : '-' : b[0] === '-' ? '-' : '');
  a = a.replace(/^-/, '');
  b = b.replace(/^-/, '');

  const res = new Array(a.length + b.length).fill(0);

  for (let i = a.length - 1; i >= 0; i--) {
    const x = +a[i];
    let carry = 0;

    for (let j = b.length - 1; j >= 0; j--) {
      const y = +b[j];
      const z = x * y + res[i+j+1] + carry;
      res[i+j+1] = z % 10;
      carry = Math.floor(z / 10);
    }
    res[i] += carry;
  }

  let i = 0;
  while (i < res.length && res[i] === 0) i++;

  let str = res.slice(i).join('');
  return sign + (str === '' ? '0' : str);
}

This function will first remove the sign from both inputs, then create a result array with the length of the sum of input lengths. Then it follows the algorithm of multiplication in which it multipies digits of a with digits of b from right to left and adds results to the result array.

Finally, it removes leading zeroes, adds the sign and returns the answer.

Time Complexity: O(n*m)

Space complexity: O(n + m)