JavaScript: Subtract two complex numbers

JavaScript Math: Exercise-51 with Solution

Write a JavaScript program to subtract 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_subtract to subtract two complex numbers.
function complex_num_subtract(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 difference.
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 subtracting the second complex number from the first.
console.log(complex_num_subtract(a,b));
```
```

Output:

```-2-10i
```

Flowchart:

Live Demo:

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

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿