dom events - JavaScript calculate function - Stack Overflow

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

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="pute" />

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

</body>
</html>

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="pute" />

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

</body>
</html>
Share Improve this question edited Apr 20, 2023 at 7:35 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 15, 2015 at 14:55 AviatorAviator 6134 gold badges11 silver badges27 bronze badges 6
  • 1 What does "work properly" mean? What does it do that you don't expect, or not do that you do expect? – Pointy 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? – doldt 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 – Aviator 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? – RobertoNovelo Commented Apr 15, 2015 at 15:02
  • becouse I need modulus also – Aviator Commented Apr 15, 2015 at 15:04
 |  Show 1 more ment

1 Answer 1

Reset to default 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="pute" />

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744835746a4596251.html

相关推荐

  • dom events - JavaScript calculate function - Stack Overflow

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

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信