为什么我的js功能提示不起作用?

时间:2018-06-17 12:39:39

标签: javascript html

我试图执行一个显示三个数字中最大的数字的函数,但要在html元素中显示它。但是要通过prompt()获取参数。 这是我的代码:



function maxNumber(num1, num2, num3) {
  var sorry = "none";

  if (num1 > num2 && num1 > num3) {
    return num1;
  } else if (num2 > num1 && num2 > num3) {
    return num2;
  } else if (num3 > num1 && num3 > num2) {
    return num3;
  } else if (num1 == num2 || num2 == num3 || num3 == num1) {
    return sorry.none;
  }
  document.getElementById("num").innerHTML = maxNumber(prompt("First Number?"), prompt("Second Number?"), prompt("Third Number?"));
}

<a href="#" onclick="maxNumber()">Which is the biggest number?</a>
<h1 id="num">Biggest number</h1>
&#13;
&#13;
&#13;

所以当我点击&#34; a&#34;标记提示应该弹出并询问数字,但它们甚至不会弹出。

2 个答案:

答案 0 :(得分:1)

您可以使用Math.max获取最大值

function maxNumber() {
  var num1 = prompt("First Number?");
  var num2 = prompt("Second Number?");
  var num3 = prompt("Third Number?");

  document.getElementById("num").innerHTML = Math.max(num1, num2, num3);
}
<a href="#" onclick="maxNumber()">Which is the biggest number?</a>
<h1 id="num">Biggest number</h1>

答案 1 :(得分:0)

&#13;
&#13;
            function maxNum() {
                var first = prompt("First Number?"),
                    second = prompt("Second Number?"),
                    third = prompt("Third Number?"),
                    maxNo = Math.max(first, second, third);
                
                document.getElementById("num").innerHTML = 'Biggest number' + maxNo;
            }
&#13;
<a href="#" onclick="maxNum()">Which is the biggest number?</a>
<h1 id="num">Biggest number</h1>
        
&#13;
&#13;
&#13;