Skip to content

Commit

Permalink
bk
Browse files Browse the repository at this point in the history
  • Loading branch information
Cule219 committed Aug 25, 2020
1 parent d6d717e commit c2a42fc
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var multiply = function (num1, num2) {
return [...num1].reverse().reduce((a, v, i) => {
let no = bigIntMultiply(num2, v) + "0".repeat(i);
return bigIntAdd([...no].reverse(), [...a].reverse());
}, "0");
};

const bigIntAdd = (bigInt1, bigInt2) => {
const lengthier = bigInt1.length >= bigInt2.length ? bigInt1 : bigInt2;
const shorter = lengthier === bigInt2 ? bigInt1 : bigInt2;
return lengthier
.reduce(
(a, v, i) => {
console.log(v, a[i], shorter[i]);
const no = +v + a[i] + shorter[i] || 0;
const over = ~~(no / 10);
a[i] = no % 10;
a[i + 1] = over;
console.log(a);
return a;
},
[0]
)
.reverse()
.join("");
};

const bigIntMultiply = (bigInt, int) => {
return [...bigInt]
.reverse()
.reduce(
(a, v, i) => {
const no = a[i] + v * int;
const over = ~~(no / 10);
a[i] = no % 10;
a[i + 1] = over;
return a;
},
[0]
)
.reverse()
.join("");
};

console.log(multiply("24", "25"));

0 comments on commit c2a42fc

Please sign in to comment.