-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path537_Complex_Number_Multiplication.js
More file actions
44 lines (38 loc) · 1.41 KB
/
537_Complex_Number_Multiplication.js
File metadata and controls
44 lines (38 loc) · 1.41 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
/*
537. Complex Number Multiplication
Given two strings representing two complex numbers.
You need to return a string representing their multiplication.Note i2 = -1 according to the definition.
Note: The input strings will not have extra blank.
The input strings will be given in the form of a + bi,
where the integer a and b will both belong to the range of[-100, 100].And the output should be also in this form.
*/
const expect = require('expect');
describe('537 Complex Number Multiplication', () => {
it('returns correct complex number', () => {
//arrange
const first = '1+1i';
const second = '1+1i';
const expected = '0+2i';
//act
const actual = complexNumberMultiply(first, second);
//assert
expect(actual).toBe(expected);
});
it('returns correct complex number', () => {
//arrange
const first = '1+-1i';
const second = '1+-1i';
const expected = '0+-2i';
//act
const actual = complexNumberMultiply(first, second);
//assert
expect(actual).toBe(expected);
});
});
const complexNumberMultiply = (first, second) => {
const firstArray = first.split('+').map(value => parseInt(value));
const secondArray = second.split('+').map(value => parseInt(value));
const real = firstArray[0] * secondArray[0] - firstArray[1] * secondArray[1];
const imaginary = firstArray[0] * secondArray[1] + firstArray[1] * secondArray[0];
return `${real}+${imaginary}i`;
};