//
// This is only a SKELETON file for the 'Complex Numbers' exercise. It's been provided as a
// convenience to get you started writing code faster.
//
export class ComplexNumber {
constructor(real, imag) {
this.real = real;
this.imag = imag;
}
add(n) {
this.real += n.real;
this.imag += n.imag;
return this;
}
sub(n) {
this.real -= n.real;
this.imag -= n.imag;
return this;
}
div(n) {
const real = (this.real\*n.real+this.imag\*n.imag)/(n.real\*n.real+n.imag\*n.imag);
this.imag = (this.imag\*n.real- this.real\*n.imag)/(n.real\*n.real+n.imag\*n.imag);
this.real = real;
return this;
}
mul(n) {
const real = this.real\*n.real-this.imag\*n.imag;
this.imag = this.imag\*n.real+this.real\*n.imag;
this.real = real;
return this;
}
get abs() {
return Math.sqrt(this.imag\*this.imag+this.real\*this.real);
}
get conj() {
return new ComplexNumber(this.real, this.imag ? -this.imag : 0)
}
get exp() {
const real = Math.exp(this.real);
return new ComplexNumber(real\*Math.cos(this.imag), real\*Math.sin(this.imag));
}
}