Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回未定义答案的Javascript基本递归输出_Javascript_Html - Fatal编程技术网

返回未定义答案的Javascript基本递归输出

返回未定义答案的Javascript基本递归输出,javascript,html,Javascript,Html,我正在尝试制作一个程序,从用户那里接收一个“base”和“exponent”,并输出“base*base*base*base”作为我们需要的指数。也就是说,2^3输出“2*2*2”。然而,我目前得到的是“未定义*未定义*未定义*未定义…”我的简单错误在哪里 我猜这是这个函数的一个小问题: function display(base, expo) { if (expo == 1) {

我正在尝试制作一个程序,从用户那里接收一个“base”和“exponent”,并输出“base*base*base*base”作为我们需要的指数。也就是说,2^3输出“2*2*2”。然而,我目前得到的是“未定义*未定义*未定义*未定义…”我的简单错误在哪里

我猜这是这个函数的一个小问题:

    function display(base, expo)
            {
                if (expo == 1)
                {
                    document.getElementById("outputfield").value = document.getElementById("outputfield").value + base;
                }

                else
                {
                    document.getElementById("outputfield").value = document.getElementById("outputfield").value + base + "*";
                    display(base, expo--)
                }
            }
以下是全部代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset = "utf-8">
        <script type = "text/javascript">
            var base = document.getElementById("base").value;
            var expo = document.getElementById("expo").value;

            function display(base, expo)
            {
                if (expo == 1)
                {
                    document.getElementById("outputfield").value = document.getElementById("outputfield").value + base;
                }

                else
                {
                    document.getElementById("outputfield").value = document.getElementById("outputfield").value + base + "*";
                    display(base, expo--)
                }
            }
        </script>
    </head>
    <body>
        <form action="#">
            <p><label>Please enter a Base Number: 
                <input id = "base" type="number"><br>
                and an Exponent:
                <input id = "expo" type ="number" ><br>
                <button id="calculate" onclick= "display()">Calculate</button>
                </label>
            </p>
            <p>
                <output id="outputfield" type = "text"></output>
            </p>
        </form>
    </body>
</html>

var base=document.getElementById(“base”).value;
var expo=document.getElementById(“expo”).value;
功能展示(基地、世博会)
{
如果(世博会==1)
{
document.getElementById(“outputfield”).value=document.getElementById(“outputfield”).value+base;
}
其他的
{
document.getElementById(“outputfield”).value=document.getElementById(“outputfield”).value+base+“*”;
展示(基地、世博会--)
}
}
请输入一个基数:

和一个指数:
算计


您可以这样定义显示功能

function display (base, expo) {
    return expo === 1 ? base.toString() : base.toString() + "*" + display(base, expo - 1);
}

document.getElementById("outputfield").value = display(base, expo);
像这样使用它

function display (base, expo) {
    return expo === 1 ? base.toString() : base.toString() + "*" + display(base, expo - 1);
}

document.getElementById("outputfield").value = display(base, expo);

当您第一次调用函数
display
时,您需要将输入值作为参数传递,然后我更喜欢使用预览函数来获取输入值,您可以在其中调用递归函数,如下所示:

var base=document.getElementById(“base”);
var expo=document.getElementById(“expo”);
var output=document.getElementById(“outputfield”);
函数解析(){
output.innerHTML=“”
output.innerHTML=显示(base.value、expo.value)
}
功能展示(基地、世博会){
return parseInt(expo)==1?base:`${base}*${display(base,--expo)}`
}

请输入一个基数:

和一个指数:
算计


声明函数中的'base'和'input'变量对我有效,但您需要在每次函数调用时重新分配'expo'输入字段的当前值

 function display(){

      var base = document.getElementById("base").value;
      var expo = document.getElementById("expo").value;

      if (expo == 1){
          document.getElementById("outputfield").value = document.getElementById("outputfield").value + base;
      }   
      else{ 
          document.getElementById("outputfield").value = document.getElementById("outputfield").value + base + "*";
          document.getElementById("expo").value = --expo;
          display(base, --expo)
      }
  }

因为您不传递任何值:
onclick=“display()”
,但您的函数需要传递的值
函数显示(base,expo)
。注意:函数参数覆盖外部作用域中具有相同名称的变量