-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.multiply.js
More file actions
48 lines (40 loc) · 1.23 KB
/
string.multiply.js
File metadata and controls
48 lines (40 loc) · 1.23 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @title: Multiply Strings
* @description: Given two non-negative integers num1 and num2
* represented as strings, return the product of num1 and num2,
* also represented as a string.
* @author: Thorsten Kober
* @email: [email protected]
*/
const multiply = function (num1, num2) {
if (num1 === '0' || num2 === '0') return '0';
const product = new Array(num1.length + num2.length);
product.fill(0);
const length = num1.length + num2.length - 1;
let position = length;
for (let i = num1.length - 1; i >= 0; i--) {
let temp = position;
for (let j = num2.length - 1; j >= 0; j--) {
product[temp] += parseInt(num1.charAt(i), 10) * parseInt(num2.charAt(j), 10);
product[temp - 1] += Math.floor(product[temp] / 10);
product[temp] %= 10;
temp--;
}
position--;
}
if (product[0] === 0) {
for (let i = 0; i <= length; i++) {
if (product[i] === 0) {
product.splice(i, 1);
if (product[i] > 0) break;
}
}
}
return product.join('');
};
// npx jest algorithms/string/string.multiply.js
test('multiply()', () => {
expect(multiply('2', '3')).toEqual('6');
expect(multiply('9', '10')).toEqual('90');
expect(multiply('123', '456')).toEqual('56088');
});