You could multiply each digit with each other digit and take the index ad postion.(您可以将每个数字彼此相乘并取索引位置。)
This approach uses the reversed arrays of the strings and reverse the result set as well.(这种方法使用字符串的反向数组,并且也反转结果集。)
function multiply(a, b) { var aa = [...a].reverse(), bb = [...b].reverse(), p = [], i, j; for (i = 0; i < aa.length; i++) { for (j = 0; j < bb.length; j++) { if (!p[i + j]) p[i + j] = 0; p[i + j] += aa[i] * bb[j]; if (p[i + j] > 9) { if (!p[i + j + 1]) p[i + j + 1] = 0; p[i + j + 1] += Math.floor(p[i + j] / 10); p[i + j] %= 10; } } } return p.reverse().join(''); } console.log(multiply('2', '3')); // 6 console.log(multiply('123', '456')); // 56088
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…