w3resource

JavaScript: Add two complex numbers

JavaScript Math: Exercise-50 with Solution

Add Two Complex Numbers

Write a JavaScript program to add two complex numbers.
A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, that satisfies the equation i2 = −1. In this expression, a is the real part and b is the imaginary part of the complex number.

Sample Solution:

JavaScript Code:

// Define a constructor function named Complex that represents complex numbers with real and imaginary parts.
function Complex(real, imaginary) {
  // Initialize real and imaginary parts of the complex number.
  this.real = 0;
  this.imaginary = 0;
  
  // Set the real part to the provided value or default to 0 if not provided.
  this.real = (typeof real === 'undefined') ? this.real : parseFloat(real);
  
  // Set the imaginary part to the provided value or default to 0 if not provided.
  this.imaginary = (typeof imaginary === 'undefined') ? this.imaginary : parseFloat(imaginary);
}

// Define a static method named transform for the Complex function to transform numbers to complex numbers.
Complex.transform = function(num) {
  var complex;
  
  // Check if the provided number is already a Complex instance.
  complex = (num instanceof Complex) ? num : complex;
  
  // Check if the provided number is a real number and convert it to a complex number with imaginary part 0.
  complex = (typeof num === 'number') ? new Complex(num, 0) : num;
  
  // Return the complex number.
  return complex;
};

// Define a function named display_complex to display complex numbers in a readable format.
function display_complex(re, im) {
  // If the imaginary part is 0, return only the real part.
  if(im === '0') return '' + re;
  
  // If the real part is 0, return only the imaginary part with 'i'.
  if(re === 0) return '' + im + 'i';
  
  // If the imaginary part is negative, return the real and imaginary parts combined with 'i'.
  if(im < 0) return '' + re + im + 'i';
  
  // Otherwise, return the real and imaginary parts combined with '+' and 'i'.
  return '' + re + '+' + im + 'i';
}

// Define a function named complex_num_add to add two complex numbers.
function complex_num_add(first, second) {
  // Transform the input numbers to complex numbers.
  var num1, num2;
  num1 = Complex.transform(first);
  num2 = Complex.transform(second);
  
  // Calculate the real and imaginary parts of the sum.
  var real = num1.real + num2.real;
  var imaginary = num1.imaginary + num2.imaginary;
  
  // Display the result as a complex number.
  return display_complex(real, imaginary);
}

// Create two complex numbers.
var a = new Complex(2, -7);
var b = new Complex(4,  3);

// Output the result of adding the two complex numbers.
console.log(complex_num_add(a,b));

Output:

6-4i

Flowchart:

JavaScript Math flowchart Pictorial-50-1

Live Demo:

See the Pen javascript-math-exercise-50 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript function to calculate Lanczos approximation gamma.
Next: Write a JavaScript program to subtract two complex numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-math-exercise-50.php