0

This JS program should return function ggt(x,z). I tried to implement it this way, everything is ok except this part I believe:

document.getElementById("demo").innerHTML=ggt(x,z); 

What should be added to make this function work properly?

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">   
    function calculate(){
        var a=document.getElementById("in1").value;
        var b=document.getElementById("in2").value;
        if(isNaN(a) || isNaN(b)){
          alert("Not a number");
        }else{
          ggt(a,b);
        }
    }
         
    function ggt(x,y){
        if (y==0){
            return x;
        }else{
            var z=x%y; 
            document.getElementById("demo").innerHTML=ggt(x,z);
            return ggt(x,z);
        }
    }
</script> 
<div>
<input id="in1" type="text" />
<input id="in2" type="text" />
<input type="button" onclick="calculate()" value="compute" />

<p id="demo"></p>
</div>

</body>
</html>
6
  • 1
    What does "work properly" mean? What does it do that you don't expect, or not do that you do expect? Commented Apr 15, 2015 at 14:56
  • Your current ggt recursion can do 2 things: return x, or not terminate at all. What are you trying to achieve? Commented Apr 15, 2015 at 15:00
  • I always get 0 as result, I am expecting function that includes variable x and modulus of the calculation those 2 numbers from input Commented Apr 15, 2015 at 15:00
  • This function is calling ggt(x,y) recursively until x%y == 0 and then returns x which makes no sense to me since x is never modified, you could simply return x? Commented Apr 15, 2015 at 15:02
  • becouse I need modulus also Commented Apr 15, 2015 at 15:04

1 Answer 1

1

function calculate()

{
  var a = document.getElementById("in1").value;
  var b = document.getElementById("in2").value;

  if (isNaN(a) || isNaN(b)) {
    alert("Not a number");
  } else {

    var values = ggt(a, b);
    
    console.log(values);

    if (values.valid) {
      document.getElementById("demonumber").innerHTML = values.number;
      document.getElementById("demomodulus").innerHTML = values.modulo;
    }
  }
}

function ggt(x, y) {
  var res = {};

  if (y == 0) {
    res.valid = false;
  } else {
    res.valid = true;
    res.number = x;
    res.modulo = x % y;
  }

  return res
}
<div>
  <input id="in1" type="text" />
  <input id="in2" type="text" />
  <input type="button" onclick="calculate()" value="compute" />

  <p id="demonumber"></p>
  <p id="demomodulus"></p>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

it should be called via function ggt(x,z) similar to my example.. but ok, tnx
ok, here is the task, can you please check it? Function "calculate" without input parameters: Read the input 1 and store value in variable a, Read the input 2 and store value in variable b, If a or b isNaN error in alert box, Otherwise call function "ggt" with parameters a and b: Function "ggt" reads parameters x and y: If y is 0, function returns x .Variable z gets the value of x modulo y. Otherwise, again calling function "ggt" with parameters x and z

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.