Search code examples
javascripthtmlinnerhtml

innerHTML to display Javascript function result


I am trying to change page content with innerHTML using the output of a specific function.

function euclid() {
      var a = document.getElementById('a');
      var b = document.getElementById('b');
      var r = [a, b]
      var j = 1, s = [1, 0], t = [0, 1], k = ["X"];
      while (r[j] > 0) {
          k.push(Math.floor(r[j-1] / r[j]));
          r.push(r[j-1] - r[j]*k[j]);
          s.push(s[j-1] - s[j]*k[j]);
          t.push(t[j-1] - t[j]*k[j]);
          j++;
      }
      var result = "gcd(" + a.value + ", " + b.value + ") = " + r[r.length - 2].value;
      document.getElementById('res').innerHTML = result;
    }

var go  = document.getElementById('go');
go.addEventListener ('click', euclid, true);
<!DOCTYPE html>

<head>
    <title>Euclids Algorithm</title>
</head>

<body>
    <h2>Euclids Algorithm</h2>

    gcd( <input type="number" id="a"> > <input type="number" id="b"> )
    <button type="button" id="go">Go!</button>
    <p id="res">To see the result please enter two numbers and press go.</p>

    <script src="euclid.js"></script>
</body>

No matter what the algorithm does not seem to modify the list r = [a, b]. Therefore when calling r[r.length - 2] (should be gcd) the program returns a.

I tried everything, but cant get my head around whats wrong. What is weird as well is, that the algorithm works in an online js playground and correctly computes the gcd.

Am I using .innerHTML correctly or are there any Javascript mishaps?

Is there anyway to debug js code with print statements (like python for example)?

New to HTML and js and thankful for any help.


Solution

  • You are not parsing the int values properly. Use parseInt to get the actual values for var a and b.

    function euclid() {
          var a = parseInt(document.getElementById('a').value);
          var b = parseInt(document.getElementById('b').value);
          var r = [a, b]
          var j = 1, s = [1, 0], t = [0, 1], k = ["X"];
          while (r[j] > 0) {
              k.push(Math.floor(r[j-1] / r[j]));
              r.push(r[j-1] - r[j]*k[j]);
              s.push(s[j-1] - s[j]*k[j]);
              t.push(t[j-1] - t[j]*k[j]);
              j++;
          }
          var result = "gcd(" + a+ ", " + b+ ") = " + r[r.length - 2];
          document.getElementById('res').innerHTML = result;
        }
    
    var go  = document.getElementById('go');
    go.addEventListener ('click', euclid, true);
    <!DOCTYPE html>
    
    <head>
        <title>Euclids Algorithm</title>
    </head>
    
    <body>
        <h2>Euclids Algorithm</h2>
    
        gcd( <input type="number" id="a"> > <input type="number" id="b"> )
        <button type="button" id="go">Go!</button>
        <p id="res">To see the result please enter two numbers and press go.</p>
    
        <script src="euclid.js"></script>
    </body>

    And, you can debug javascript code directly from your browser's console. Read more How can I debug my JavaScript code?