multiplication of complex numbers javascript

Solutions on MaxInterview for multiplication of complex numbers javascript by the best coders in the world

showing results for - "multiplication of complex numbers javascript"
Nola
28 Aug 2018
1function Complex(real, imaginary) {
2  this.real = 0;
3  this.imaginary = 0;
4  this.real = (typeof real === 'undefined') ? this.real : parseFloat(real);
5  this.imaginary = (typeof imaginary === 'undefined') ? this.imaginary : parseFloat(imaginary);
6}
7Complex.transform = function(num) {
8  var complex;
9  complex = (num instanceof Complex) ? num : complex;
10  complex = (typeof num === 'number') ? new Complex(num, 0) : num;
11  return complex;
12};
13function display_complex(re, im) {
14  if(im === '0') return '' + re;
15  if(re === 0) return '' + im + 'i';
16  if(im < 0) return '' + re + im + 'i';
17  return '' + re + '+' + im + 'i';
18}
19function complex_num_add(first, second) {
20  var num1, num2;
21  num1 = Complex.transform(first);
22  num2 = Complex.transform(second);
23  var real = num1.real + num2.real;
24  var imaginary = num1.imaginary + num2.imaginary;
25  return display_complex(real, imaginary);
26}
27 var a = new Complex(2, -7);
28 var b = new Complex(4,  3);
29console.log(complex_num_add(a,b));
30
31