-
Notifications
You must be signed in to change notification settings - Fork 4
/
MultiplyingNumbersAsStrings.js
32 lines (32 loc) · 1.07 KB
/
MultiplyingNumbersAsStrings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Multiply two numbers! Simple!
*
* The arguments are passed as strings.
* The numbers may be way very large
* Answer should be returned as a string
* The returned "number" should not start with zeros e.g. 0123 is invalid
*/
function multiply(a, b) {
const aRev = Array.from(a.toString()).reverse()
, bRev = Array.from(b.toString()).reverse()
, res = []
, reversedMultiply = (k, currM, currA) => {
res[k] = currM + currA
if (res[k] > 9) {
const prevD = Math.floor(res[k] / 10)
, prevA = k + 1 >= res.length ? 0 : res[k + 1]
res[k + 1] = prevD + prevA
res[k] -= prevD * 10
}
}
, prepareLoop = (currA, aI) => {
bRev.map((currB, bI) => {
const keyI = aI + bI
, multiplyCurrent = currA * currB
, additionCurrent = keyI >= res.length ? 0 : res[keyI]
reversedMultiply(keyI, multiplyCurrent, additionCurrent)
})
}
aRev.map(prepareLoop)
return res.reverse().join('').replace(/^0+/, '') || '0'
}