Calculate Complex Number Expression


this code is created first by chatGPT on Jan 8 2023 (meaning using chatGPT 3.5) and then modified a little bit my me. The initial request that I put into chatGPT is as follows :


Write a javascript with the following requirement

1. create a table at the center of the page

2. the table has two rows and one column, make grid visible in thin line

3. put a textbox that can take in a math expression. the default value of the text box is '100*abs(cos(t))' and put a button with 'plot'

4. put a canvas that plot the data in polar coordinate in the lower row. Do not put any background in the canvas. the background should be all white.

5. make the edge of the canvas visible

6. plot axis lines at (0,0) in the plot in black

7. plot a grid lines in the plot in gray in polar coordinate.

8. javascript for the plot should be stored in a separate file

9. if you want to use style, create a separate css file.

10. Create an html file that combines all of these components

NOTE : It is not guaranteed that you would have the same code as I got since chatGPT produce the answers differently depending on the context. And it may produce the different answers everytime you ask even with the exact the same question.

NOTE : If you don't have any of your own idea for the request, copy my request and paste it into the chatGPT and put additional requests based on the output for the previous request. I would suggest to create a new thread in the chatGPT and put my request and then continue to add your own request.



Complex 1 Operator Complex 2 = Result

NOTE : When you write 'imaginary part' of the number, you should not obmit the '1'. For example, 1 + j would cause an error. You should write 1 + 1j instead.





    <title>Complex Number Calculator</title>



    <table style="border: thin solid; margin: auto;">


        <th>Complex 1</th>


        <th>Complex 2</th>






          <input type="text" id="Complex1" value="1 + 1j">




          <select id="Operator">

            <option value="+">+</option>

            <option value="-">-</option>

            <option value="*">*</option>

            <option value="/">/</option>





          <input type="text" id="Complex2" value="1 + 2j">



          <button id="Calculate" onclick="CalcExpr()">Calculate</button>



          <input type="text" id="Result">




    <script src="CalcComplex.js"></script>






function CalcExpr() {

  // Get the Complex1, Complex2, and Operator values

  const complex1 = document.getElementById("Complex1").value;

  const complex2 = document.getElementById("Complex2").value;

  const operator = document.getElementById("Operator").value;


  // Perform the calculation and set the result in the Result textbox

  const resultTextbox = document.getElementById("Result");

  resultTextbox.value = calculate(complex1, complex2, operator);



function calculate(complex1, complex2, operator) {

  // Parse the complex numbers into real and imaginary components

  const complex1Regex = /^([+-]?\d+)\s*([+-])\s*(\d+)j$/;

  const complex1Match = complex1.match(complex1Regex);

  const complex1Real = parseInt(complex1Match[1], 10);

  const complex1Imag = parseInt(complex1Match[3], 10);

  const complex1Sign = complex1Match[2];

  if (complex1Sign === "-") {

    complex1Imag *= -1;



  const complex2Regex = /^([+-]?\d+)\s*([+-])\s*(\d+)j$/;

  const complex2Match = complex2.match(complex2Regex);

  const complex2Real = parseInt(complex2Match[1], 10);

  const complex2Imag = parseInt(complex2Match[3], 10);

  const complex2Sign = complex2Match[2];

  if (complex2Sign === "-") {

    complex2Imag *= -1;



  // Perform the calculation

  let resultReal;

  let resultImag;

  switch (operator) {

    case "+":

      resultReal = complex1Real + complex2Real;

      resultImag = complex1Imag + complex2Imag;


    case "-":

      resultReal = complex1Real - complex2Real;

      resultImag = complex1Imag - complex2Imag;


    case "*":

      resultReal = complex1Real * complex2Real - complex1Imag * complex2Imag;

      resultImag = complex1Real * complex2Imag + complex1Imag * complex2Real;


    case "/":

      resultReal = (complex1Real * complex2Real + complex1Imag * complex2Imag) / (complex2Real * complex2Real + complex2Imag * complex2Imag);

      resultImag = (complex1Imag * complex2Real - complex1Real * complex2Imag) / (complex2Real * complex2Real + complex2Imag * complex2Imag);



      throw new Error(`Invalid operator: ${operator}`);



  // Format the result as a string

  let resultSign = "+";

  if (resultImag < 0) {

    resultSign = "-";

    resultImag *= -1;


  return `${resultReal} ${resultSign} ${resultImag}j`;